9d474958bbe10763fc81c4b5d9a599a936ee7dbd
[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     // we set default access right to be 0660.
64     // TODO need a way to allow this to be changed
65     
66     fsapi_inode_setaccess(inode, FSACL_DEFAULT);
67     fsapi_inode_setowner(inode, 0, 0);
68 }
69
70 int
71 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
72 {
73     vsb->ops.init_inode = __twifs_init_inode;
74
75     struct v_inode* inode = vfs_i_alloc(vsb);
76     if (!inode) {
77         return ENOMEM;
78     }
79
80     inode->id = fs_root->ino_id;
81     inode->itype = fs_root->itype;
82     inode->data = fs_root;
83
84     vfs_i_addhash(inode);
85     vfs_assign_inode(mount_point, inode);
86     return 0;
87 }
88
89 int
90 __twifs_unmount(struct v_superblock* vsb)
91 {
92     return 0;
93 }
94
95 int
96 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
97 {
98     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
99     if (!twi_node || !twi_node->ops.write) {
100         return ENOTSUP;
101     }
102     return twi_node->ops.write(inode, buffer, len, fpos);
103 }
104
105 int
106 __twifs_fwrite_pg(struct v_inode* inode, void* buffer, size_t fpos)
107 {
108     return __twifs_fwrite(inode, buffer, PAGE_SIZE, fpos);
109 }
110
111 int
112 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
113 {
114     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
115     if (!twi_node || !twi_node->ops.read) {
116         return ENOTSUP;
117     }
118     return twi_node->ops.read(inode, buffer, len, fpos);
119 }
120
121 int
122 __twifs_fread_pg(struct v_inode* inode, void* buffer, size_t fpos)
123 {
124     return __twifs_fread(inode, buffer, PAGE_SIZE, fpos);
125 }
126
127 struct twifs_node*
128 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
129 {
130     if (!parent)
131         return NULL;
132
133     struct twifs_node *pos, *n;
134     llist_for_each(pos, n, &parent->children, siblings)
135     {
136         if (HSTR_EQ(&pos->name, name)) {
137             return pos;
138         }
139     }
140     return NULL;
141 }
142
143 int
144 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
145 {
146     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
147
148     if (!check_directory_node(inode)) {
149         return ENOTDIR;
150     }
151
152     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
153     if (child_node) {
154         struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
155         if (child_inode) {
156             goto done;
157         }
158
159         if (!(child_inode = vfs_i_alloc(inode->sb))) {
160             return ENOENT;
161         }
162
163         child_inode->id = child_node->ino_id;
164         child_inode->itype = child_node->itype;
165         child_inode->data = child_node;
166
167         vfs_i_addhash(child_inode);
168
169     done:
170         dnode->data = child_node->data;
171         vfs_assign_inode(dnode, child_inode);
172         return 0;
173     }
174     return ENOENT;
175 }
176
177 int
178 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
179 {
180     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
181     unsigned int counter = 2;
182     struct twifs_node *pos, *n;
183
184     if (fsapi_handle_pseudo_dirent(file, dctx)) {
185         return 1;
186     }
187
188     llist_for_each(pos, n, &twi_node->children, siblings)
189     {
190         if (counter++ >= file->f_pos) {
191             fsapi_dir_report(
192               dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
193             return 1;
194         }
195     }
196
197     return 0;
198 }
199
200 int
201 __twifs_openfile(struct v_inode* inode, struct v_file* file)
202 {
203     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
204     if (twi_node) {
205         return 0;
206     }
207     return ENOTSUP;
208 }
209
210 int
211 twifs_rm_node(struct twifs_node* node)
212 {
213     if (check_itype(node->itype, VFS_IFDIR) && !llist_empty(&node->children)) {
214         return ENOTEMPTY;
215     }
216     llist_delete(&node->siblings);
217     cake_release(twi_pile, node);
218     return 0;
219 }
220
221 struct twifs_node*
222 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
223 {
224     char buf[VFS_NAME_MAXLEN];
225     size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
226
227     return __twifs_new_node(parent ? parent : fs_root, buf, len, F_FILE);
228 }
229
230 struct twifs_node*
231 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
232 {
233     va_list args;
234     va_start(args, fmt);
235
236     struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
237
238     va_end(args);
239
240     return twi_node;
241 }
242
243 struct twifs_node*
244 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
245 {
246     va_list args;
247     va_start(args, fmt);
248
249     char buf[VFS_NAME_MAXLEN];
250     size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
251     struct twifs_node* twi_node =
252       __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
253
254     va_end(args);
255
256     return twi_node;
257 }
258
259 void
260 twifs_init()
261 {
262     struct filesystem* fs;
263     fs = fsapi_fs_declare("twifs", FSTYPE_PSEUDO | FSTYPE_ROFS);
264     
265     fsapi_fs_set_mntops(fs, __twifs_mount, __twifs_unmount);
266     fsapi_fs_finalise(fs);
267
268     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
269     fs_root = twifs_dir_node(NULL, NULL, 0, 0);
270 }
271 EXPORT_FILE_SYSTEM(twifs, twifs_init);
272
273 void
274 twifs_register_plugins()
275 {
276     ldga_invoke_fn0(twiplugin_inits);
277 }
278
279 int
280 __twifs_twimap_file_read(struct v_inode* inode,
281                          void* buf,
282                          size_t len,
283                          size_t fpos)
284 {
285     struct twimap* map = twinode_getdata(inode, struct twimap*);
286     return twimap_read(map, buf, len, fpos);
287 }
288
289 struct twimap*
290 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
291 {
292     va_list args;
293     va_start(args, fmt);
294
295     struct twimap* map = twimap_create(data);
296     struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
297     node->ops.read = __twifs_twimap_file_read;
298     node->data = map;
299
300     return map;
301 }
302
303 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
304                                            .read = __twifs_fread,
305                                            .read_page = __twifs_fread_pg,
306                                            .write = __twifs_fwrite,
307                                            .write_page = __twifs_fwrite_pg,
308                                            .readdir = __twifs_iterate_dir };
309
310 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
311                                              .mkdir = default_inode_mkdir,
312                                              .rmdir = default_inode_rmdir,
313                                              .open = __twifs_openfile };