feat: (twimap) provide an easy way for mapping kernel objects into filesystem
[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/mm/cake.h>
17 #include <lunaix/mm/valloc.h>
18 #include <lunaix/spike.h>
19
20 static struct twifs_node* fs_root;
21
22 static struct cake_pile* twi_pile;
23
24 static volatile uint32_t inode_id = 0;
25
26 extern const struct v_file_ops twifs_file_ops;
27 extern const struct v_inode_ops twifs_inode_ops;
28
29 struct twifs_node*
30 __twifs_new_node(struct twifs_node* parent,
31                  const char* name,
32                  int name_len,
33                  uint32_t itype)
34 {
35     struct twifs_node* node = cake_grab(twi_pile);
36     memset(node, 0, sizeof(*node));
37
38     strncpy(node->name_val, name, VFS_NAME_MAXLEN);
39
40     node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
41     node->itype = itype;
42     node->ino_id = inode_id++;
43     hstr_rehash(&node->name, HSTR_FULL_HASH);
44     llist_init_head(&node->children);
45
46     if (parent) {
47         llist_append(&parent->children, &node->siblings);
48     }
49
50     return node;
51 }
52
53 void
54 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
55 {
56     inode->ops = &twifs_inode_ops;
57     inode->default_fops = &twifs_file_ops;
58 }
59
60 int
61 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
62 {
63     vsb->dev = 1;
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, 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 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
246                                            .read = __twifs_fread,
247                                            .write = __twifs_fwrite,
248                                            .readdir = __twifs_iterate_dir };
249
250 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
251                                              .mkdir = default_inode_mkdir,
252                                              .rmdir = default_inode_rmdir,
253                                              .open = __twifs_openfile };