Code-base clean-up and refactoring (#47)
[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/strfmt.h>
12 #include <klibc/string.h>
13 #include <lunaix/clock.h>
14 #include <lunaix/fs/api.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 #include <asm/pagetable.h>
22
23 static struct twifs_node* fs_root;
24
25 static struct cake_pile* twi_pile;
26
27 static volatile u32_t inode_id = 0;
28
29 extern const struct v_file_ops twifs_file_ops;
30 extern const struct v_inode_ops twifs_inode_ops;
31
32 struct twifs_node*
33 __twifs_new_node(struct twifs_node* parent,
34                  const char* name,
35                  int name_len,
36                  u32_t itype)
37 {
38     struct twifs_node* node = cake_grab(twi_pile);
39     memset(node, 0, sizeof(*node));
40
41     strncpy(node->name_val, name, VFS_NAME_MAXLEN);
42
43     node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
44     node->itype = itype;
45     node->ino_id = inode_id++;
46     hstr_rehash(&node->name, HSTR_FULL_HASH);
47     llist_init_head(&node->children);
48
49     if (parent) {
50         llist_append(&parent->children, &node->siblings);
51     }
52
53     return node;
54 }
55
56 void
57 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
58 {
59     inode->ops = (struct v_inode_ops*)&twifs_inode_ops;
60     inode->default_fops = (struct v_file_ops*)&twifs_file_ops;
61 }
62
63 int
64 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
65 {
66     vsb->ops.init_inode = __twifs_init_inode;
67
68     struct v_inode* inode = vfs_i_alloc(vsb);
69     if (!inode) {
70         return ENOMEM;
71     }
72
73     inode->id = fs_root->ino_id;
74     inode->itype = fs_root->itype;
75     inode->data = fs_root;
76
77     vfs_i_addhash(inode);
78     vfs_assign_inode(mount_point, inode);
79     return 0;
80 }
81
82 int
83 __twifs_unmount(struct v_superblock* vsb)
84 {
85     return 0;
86 }
87
88 int
89 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
90 {
91     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
92     if (!twi_node || !twi_node->ops.write) {
93         return ENOTSUP;
94     }
95     return twi_node->ops.write(inode, buffer, len, fpos);
96 }
97
98 int
99 __twifs_fwrite_pg(struct v_inode* inode, void* buffer, size_t fpos)
100 {
101     return __twifs_fwrite(inode, buffer, PAGE_SIZE, fpos);
102 }
103
104 int
105 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
106 {
107     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
108     if (!twi_node || !twi_node->ops.read) {
109         return ENOTSUP;
110     }
111     return twi_node->ops.read(inode, buffer, len, fpos);
112 }
113
114 int
115 __twifs_fread_pg(struct v_inode* inode, void* buffer, size_t fpos)
116 {
117     return __twifs_fread(inode, buffer, PAGE_SIZE, fpos);
118 }
119
120 struct twifs_node*
121 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
122 {
123     if (!parent)
124         return NULL;
125
126     struct twifs_node *pos, *n;
127     llist_for_each(pos, n, &parent->children, siblings)
128     {
129         if (HSTR_EQ(&pos->name, name)) {
130             return pos;
131         }
132     }
133     return NULL;
134 }
135
136 int
137 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
138 {
139     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
140
141     if (!check_directory_node(inode)) {
142         return ENOTDIR;
143     }
144
145     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
146     if (child_node) {
147         struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
148         if (child_inode) {
149             goto done;
150         }
151
152         if (!(child_inode = vfs_i_alloc(inode->sb))) {
153             return ENOENT;
154         }
155
156         child_inode->id = child_node->ino_id;
157         child_inode->itype = child_node->itype;
158         child_inode->data = child_node;
159
160         vfs_i_addhash(child_inode);
161
162     done:
163         dnode->data = child_node->data;
164         vfs_assign_inode(dnode, child_inode);
165         return 0;
166     }
167     return ENOENT;
168 }
169
170 int
171 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
172 {
173     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
174     unsigned int counter = 2;
175     struct twifs_node *pos, *n;
176
177     if (fsapi_handle_pseudo_dirent(file, dctx)) {
178         return 1;
179     }
180
181     llist_for_each(pos, n, &twi_node->children, siblings)
182     {
183         if (counter++ >= file->f_pos) {
184             fsapi_dir_report(
185               dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
186             return 1;
187         }
188     }
189
190     return 0;
191 }
192
193 int
194 __twifs_openfile(struct v_inode* inode, struct v_file* file)
195 {
196     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
197     if (twi_node) {
198         return 0;
199     }
200     return ENOTSUP;
201 }
202
203 int
204 twifs_rm_node(struct twifs_node* node)
205 {
206     if (check_itype(node->itype, VFS_IFDIR) && !llist_empty(&node->children)) {
207         return ENOTEMPTY;
208     }
209     llist_delete(&node->siblings);
210     cake_release(twi_pile, node);
211     return 0;
212 }
213
214 struct twifs_node*
215 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
216 {
217     char buf[VFS_NAME_MAXLEN];
218     size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
219
220     return __twifs_new_node(parent ? parent : fs_root, buf, len, F_FILE);
221 }
222
223 struct twifs_node*
224 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
225 {
226     va_list args;
227     va_start(args, fmt);
228
229     struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
230
231     va_end(args);
232
233     return twi_node;
234 }
235
236 struct twifs_node*
237 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
238 {
239     va_list args;
240     va_start(args, fmt);
241
242     char buf[VFS_NAME_MAXLEN];
243     size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
244     struct twifs_node* twi_node =
245       __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
246
247     va_end(args);
248
249     return twi_node;
250 }
251
252 void
253 twifs_init()
254 {
255     struct filesystem* fs;
256     fs = fsapi_fs_declare("twifs", FSTYPE_PSEUDO | FSTYPE_ROFS);
257     
258     fsapi_fs_set_mntops(fs, __twifs_mount, __twifs_unmount);
259     fsapi_fs_finalise(fs);
260
261     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
262     fs_root = twifs_dir_node(NULL, NULL, 0, 0);
263 }
264 EXPORT_FILE_SYSTEM(twifs, twifs_init);
265
266 void
267 twifs_register_plugins()
268 {
269     ldga_invoke_fn0(twiplugin_inits);
270 }
271
272 int
273 __twifs_twimap_file_read(struct v_inode* inode,
274                          void* buf,
275                          size_t len,
276                          size_t fpos)
277 {
278     struct twimap* map = twinode_getdata(inode, struct twimap*);
279     return twimap_read(map, buf, len, fpos);
280 }
281
282 struct twimap*
283 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
284 {
285     va_list args;
286     va_start(args, fmt);
287
288     struct twimap* map = twimap_create(data);
289     struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
290     node->ops.read = __twifs_twimap_file_read;
291     node->data = map;
292
293     return map;
294 }
295
296 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
297                                            .read = __twifs_fread,
298                                            .read_page = __twifs_fread_pg,
299                                            .write = __twifs_fwrite,
300                                            .write_page = __twifs_fwrite_pg,
301                                            .readdir = __twifs_iterate_dir };
302
303 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
304                                              .mkdir = default_inode_mkdir,
305                                              .rmdir = default_inode_rmdir,
306                                              .open = __twifs_openfile };