laying out the iso9660 structure definitions
[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 uint32_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                  uint32_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 = &twifs_inode_ops;
58     inode->default_fops = &twifs_file_ops;
59 }
60
61 int
62 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
63 {
64     vsb->dev = 1;
65     vsb->ops.init_inode = __twifs_init_inode;
66
67     struct v_inode* inode = vfs_i_alloc(vsb);
68     if (!inode) {
69         return ENOMEM;
70     }
71
72     inode->id = fs_root->ino_id;
73     inode->itype = fs_root->itype;
74     inode->data = fs_root;
75
76     vfs_i_addhash(inode);
77     vfs_assign_inode(mount_point, inode);
78     return 0;
79 }
80
81 int
82 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
83 {
84     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
85     if (!twi_node || !twi_node->ops.write) {
86         return ENOTSUP;
87     }
88     return twi_node->ops.write(inode, buffer, len, fpos);
89 }
90
91 int
92 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
93 {
94     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
95     if (!twi_node || !twi_node->ops.read) {
96         return ENOTSUP;
97     }
98     return twi_node->ops.read(inode, buffer, len, fpos);
99 }
100
101 struct twifs_node*
102 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
103 {
104     if (!parent)
105         return NULL;
106
107     struct twifs_node *pos, *n;
108     llist_for_each(pos, n, &parent->children, siblings)
109     {
110         if (HSTR_EQ(&pos->name, name)) {
111             return pos;
112         }
113     }
114     return NULL;
115 }
116
117 int
118 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
119 {
120     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
121
122     if (!(twi_node->itype & VFS_IFDIR)) {
123         return ENOTDIR;
124     }
125
126     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
127     if (child_node) {
128         struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
129         if (child_inode) {
130             goto done;
131         }
132
133         if (!(child_inode = vfs_i_alloc(inode->sb))) {
134             return ENOENT;
135         }
136
137         child_inode->id = child_node->ino_id;
138         child_inode->itype = child_node->itype;
139         child_inode->data = child_node;
140
141         vfs_i_addhash(child_inode);
142
143     done:
144         dnode->data = child_node->data;
145         vfs_assign_inode(dnode, child_inode);
146         return 0;
147     }
148     return ENOENT;
149 }
150
151 int
152 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
153 {
154     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
155     int counter = 0;
156     struct twifs_node *pos, *n;
157
158     llist_for_each(pos, n, &twi_node->children, siblings)
159     {
160         if (counter++ >= dctx->index) {
161             dctx->index = counter;
162             dctx->read_complete_callback(
163               dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
164             return 1;
165         }
166     }
167
168     return 0;
169 }
170
171 int
172 __twifs_openfile(struct v_inode* inode, struct v_file* file)
173 {
174     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
175     if (twi_node) {
176         return 0;
177     }
178     return ENOTSUP;
179 }
180
181 int
182 twifs_rm_node(struct twifs_node* node)
183 {
184     if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
185         return ENOTEMPTY;
186     }
187     llist_delete(&node->siblings);
188     cake_release(twi_pile, node);
189     return 0;
190 }
191
192 struct twifs_node*
193 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
194 {
195     char buf[VFS_NAME_MAXLEN];
196     size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
197
198     return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV);
199 }
200
201 struct twifs_node*
202 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
203 {
204     va_list args;
205     va_start(args, fmt);
206
207     struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
208
209     va_end(args);
210
211     return twi_node;
212 }
213
214 struct twifs_node*
215 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
216 {
217     va_list args;
218     va_start(args, fmt);
219
220     char buf[VFS_NAME_MAXLEN];
221     size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
222     struct twifs_node* twi_node =
223       __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
224
225     va_end(args);
226
227     return twi_node;
228 }
229
230 void
231 twifs_init()
232 {
233     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
234
235     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
236     twifs->fs_name = HSTR("twifs", 5);
237     twifs->mount = __twifs_mount;
238     twifs->types = FSTYPE_ROFS;
239     twifs->fs_id = 0;
240
241     fsm_register(twifs);
242
243     fs_root = twifs_dir_node(NULL, NULL, 0, 0);
244 }
245
246 int
247 __twifs_twimap_file_read(struct v_inode* inode,
248                          void* buf,
249                          size_t len,
250                          size_t fpos)
251 {
252     struct twimap* map = twinode_getdata(inode, struct twimap*);
253     return twimap_read(map, buf, len, fpos);
254 }
255
256 struct twimap*
257 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
258 {
259     va_list args;
260     va_start(args, fmt);
261
262     struct twimap* map = twimap_create(data);
263     struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
264     node->ops.read = __twifs_twimap_file_read;
265     node->data = map;
266
267     return map;
268 }
269
270 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
271                                            .read = __twifs_fread,
272                                            .write = __twifs_fwrite,
273                                            .readdir = __twifs_iterate_dir };
274
275 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
276                                              .mkdir = default_inode_mkdir,
277                                              .rmdir = default_inode_rmdir,
278                                              .open = __twifs_openfile };