refactor: add a simple ramfs for rootfs. Twifs should have more specific job in future.
[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/string.h>
12 #include <lunaix/clock.h>
13 #include <lunaix/fs.h>
14 #include <lunaix/fs/twifs.h>
15 #include <lunaix/mm/cake.h>
16 #include <lunaix/mm/valloc.h>
17
18 static struct twifs_node* fs_root;
19
20 static struct cake_pile* twi_pile;
21
22 static volatile uint32_t inode_id = 0;
23
24 extern const struct v_file_ops twifs_file_ops;
25 extern const struct v_inode_ops twifs_inode_ops;
26
27 struct twifs_node*
28 __twifs_new_node(struct twifs_node* parent,
29                  const char* name,
30                  int name_len,
31                  uint32_t itype)
32 {
33     struct twifs_node* node = cake_grab(twi_pile);
34     memset(node, 0, sizeof(*node));
35
36     node->name = HSTR(name, name_len);
37     node->itype = itype;
38     node->ino_id = inode_id++;
39     hstr_rehash(&node->name, HSTR_FULL_HASH);
40     llist_init_head(&node->children);
41
42     if (parent) {
43         llist_append(&parent->children, &node->siblings);
44     }
45
46     return node;
47 }
48
49 void
50 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
51 {
52     inode->ops = &twifs_inode_ops;
53     inode->default_fops = &twifs_file_ops;
54 }
55
56 int
57 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
58 {
59     vsb->dev = 1;
60     vsb->ops.init_inode = __twifs_init_inode;
61
62     struct v_inode* inode = vfs_i_alloc(vsb);
63     if (!inode) {
64         return ENOMEM;
65     }
66
67     inode->id = fs_root->ino_id;
68     inode->itype = fs_root->itype;
69     inode->data = fs_root;
70
71     vfs_i_addhash(inode);
72     vfs_assign_inode(mount_point, inode);
73     return 0;
74 }
75
76 int
77 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
78 {
79     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
80     if (!twi_node || !twi_node->ops.write) {
81         return ENOTSUP;
82     }
83     return twi_node->ops.write(inode, buffer, len, fpos);
84 }
85
86 int
87 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
88 {
89     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
90     if (!twi_node || !twi_node->ops.read) {
91         return ENOTSUP;
92     }
93     return twi_node->ops.read(inode, buffer, len, fpos);
94 }
95
96 struct twifs_node*
97 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
98 {
99     if (!parent)
100         return NULL;
101
102     struct twifs_node *pos, *n;
103     llist_for_each(pos, n, &parent->children, siblings)
104     {
105         if (HSTR_EQ(&pos->name, name)) {
106             return pos;
107         }
108     }
109     return NULL;
110 }
111
112 int
113 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
114 {
115     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
116
117     if (!(twi_node->itype & VFS_IFDIR)) {
118         return ENOTDIR;
119     }
120
121     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
122     if (child_node) {
123         struct v_inode* inode = vfs_i_find(inode->sb, child_node->ino_id);
124         if (inode) {
125             goto done;
126         }
127
128         if (!(inode = vfs_i_alloc(inode->sb))) {
129             return ENOENT;
130         }
131
132         inode->id = child_node->ino_id;
133         inode->itype = child_node->itype;
134         inode->data = child_node;
135
136         vfs_i_addhash(inode);
137
138     done:
139         dnode->data = child_node->data;
140         vfs_assign_inode(dnode, inode);
141         return 0;
142     }
143     return ENOENT;
144 }
145
146 int
147 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
148 {
149     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
150     int counter = 0;
151     struct twifs_node *pos, *n;
152
153     llist_for_each(pos, n, &twi_node->children, siblings)
154     {
155         if (counter++ >= dctx->index) {
156             dctx->index = counter;
157             dctx->read_complete_callback(
158               dctx, pos->name.value, pos->name.len, pos->itype);
159             return 1;
160         }
161     }
162
163     return 0;
164 }
165
166 int
167 __twifs_openfile(struct v_inode* inode, struct v_file* file)
168 {
169     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
170     if (twi_node) {
171         return 0;
172     }
173     return ENOTSUP;
174 }
175
176 int
177 twifs_rm_node(struct twifs_node* node)
178 {
179     if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
180         return ENOTEMPTY;
181     }
182     llist_delete(&node->siblings);
183     cake_release(twi_pile, node);
184     return 0;
185 }
186
187 struct twifs_node*
188 twifs_file_node(struct twifs_node* parent,
189                 const char* name,
190                 int name_len,
191                 uint32_t itype)
192 {
193     struct twifs_node* twi_node =
194       __twifs_new_node(parent, name, name_len, VFS_IFFILE | itype);
195
196     return twi_node;
197 }
198
199 struct twifs_node*
200 twifs_dir_node(struct twifs_node* parent,
201                const char* name,
202                int name_len,
203                uint32_t itype)
204 {
205     struct hstr hname = HSTR(name, name_len);
206     hstr_rehash(&hname, HSTR_FULL_HASH);
207     struct twifs_node* node = __twifs_get_node(parent, &hname);
208     if (node) {
209         return node;
210     }
211
212     struct twifs_node* twi_node =
213       __twifs_new_node(parent, name, name_len, VFS_IFDIR | itype);
214
215     return twi_node;
216 }
217
218 struct twifs_node*
219 twifs_toplevel_node(const char* name, int name_len, uint32_t itype)
220 {
221     return twifs_dir_node(fs_root, name, name_len, itype);
222 }
223
224 void
225 twifs_init()
226 {
227     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
228
229     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
230     twifs->fs_name = HSTR("twifs", 5);
231     twifs->mount = __twifs_mount;
232     twifs->types = FSTYPE_ROFS;
233     twifs->fs_id = 0;
234
235     fsm_register(twifs);
236
237     fs_root = twifs_dir_node(NULL, NULL, 0, 0);
238 }
239
240 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
241                                            .read = __twifs_fread,
242                                            .write = __twifs_fwrite,
243                                            .readdir = __twifs_iterate_dir };
244
245 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
246                                              .mkdir = default_inode_mkdir,
247                                              .rmdir = default_inode_rmdir,
248                                              .open = __twifs_openfile };