104a2fba8e9edceb4e0ce0f68d582210f7ffd01e
[lunaix-os.git] / lunaix-os / kernel / fs / 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 int
23 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode);
24
25 int
26 __twifs_openfile(struct v_inode* inode, struct v_file* file);
27
28 struct twifs_node*
29 __twifs_get_node(struct twifs_node* parent, struct hstr* name);
30
31 struct v_inode*
32 __twifs_create_inode(struct twifs_node* twi_node);
33
34 int
35 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx);
36
37 int
38 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point);
39
40 int
41 __twifs_mkdir(struct v_inode* inode, struct v_dnode* dnode);
42
43 void
44 twifs_init()
45 {
46     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
47
48     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
49     twifs->fs_name = HSTR("twifs", 5);
50     twifs->mount = __twifs_mount;
51
52     fsm_register(twifs);
53
54     fs_root = twifs_dir_node(NULL, NULL, 0);
55 }
56
57 struct twifs_node*
58 __twifs_new_node(struct twifs_node* parent, const char* name, int name_len)
59 {
60     struct twifs_node* node = cake_grab(twi_pile);
61     memset(node, 0, sizeof(*node));
62
63     node->name = HSTR(name, name_len);
64     hstr_rehash(&node->name, HSTR_FULL_HASH);
65     llist_init_head(&node->children);
66
67     if (parent) {
68         llist_append(&parent->children, &node->siblings);
69     }
70
71     return node;
72 }
73
74 void
75 twifs_rm_node(struct twifs_node* node)
76 {
77     if ((node->itype & VFS_INODE_TYPE_DIR)) {
78         // TODO recursivly delete any sub-directories.
79     }
80     llist_delete(&node->siblings);
81     vfs_i_free(node->inode);
82     cake_release(twi_pile, node);
83 }
84
85 struct twifs_node*
86 twifs_file_node(struct twifs_node* parent, const char* name, int name_len)
87 {
88     struct twifs_node* twi_node = __twifs_new_node(parent, name, name_len);
89     twi_node->itype = VFS_INODE_TYPE_FILE;
90
91     struct v_inode* twi_inode = __twifs_create_inode(twi_node);
92     twi_node->inode = twi_inode;
93
94     return twi_node;
95 }
96
97 struct twifs_node*
98 twifs_dir_node(struct twifs_node* parent, const char* name, int name_len)
99 {
100     struct hstr hname = HSTR(name, name_len);
101     hstr_rehash(&hname, HSTR_FULL_HASH);
102     struct twifs_node* node = __twifs_get_node(parent, &hname);
103     if (node) {
104         return node;
105     }
106
107     struct twifs_node* twi_node = __twifs_new_node(parent, name, name_len);
108     twi_node->itype = VFS_INODE_TYPE_DIR;
109
110     struct v_inode* twi_inode = __twifs_create_inode(twi_node);
111     twi_node->fops.readdir = __twifs_iterate_dir;
112     twi_node->inode = twi_inode;
113
114     return twi_node;
115 }
116
117 struct twifs_node*
118 twifs_toplevel_node(const char* name, int name_len)
119 {
120     return twifs_dir_node(fs_root, name, name_len);
121 }
122
123 int
124 __twifs_mkdir(struct v_inode* inode, struct v_dnode* dnode)
125 {
126     struct twifs_node* parent_node = (struct twifs_node*)inode->data;
127     if (!(parent_node->itype & VFS_INODE_TYPE_DIR)) {
128         return ENOTDIR;
129     }
130     struct twifs_node* new_node =
131       twifs_dir_node(parent_node, dnode->name.value, dnode->name.len);
132     dnode->inode = new_node->inode;
133
134     return 0;
135 }
136
137 int
138 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
139 {
140     mount_point->inode = fs_root->inode;
141     return 0;
142 }
143
144 struct v_inode*
145 __twifs_create_inode(struct twifs_node* twi_node)
146 {
147     struct v_inode* inode = vfs_i_alloc();
148     *inode = (struct v_inode){ .ctime = 0,
149                                .itype = twi_node->itype,
150                                .lb_addr = 0,
151                                .lb_usage = 0,
152                                .data = twi_node,
153                                .mtime = 0,
154                                .ref_count = 0 };
155     inode->ops.dir_lookup = __twifs_dirlookup;
156     inode->ops.mkdir = __twifs_mkdir;
157     inode->ops.open = __twifs_openfile;
158
159     return inode;
160 }
161
162 struct twifs_node*
163 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
164 {
165     if (!parent)
166         return NULL;
167
168     struct twifs_node *pos, *n;
169     llist_for_each(pos, n, &parent->children, siblings)
170     {
171         if (HSTR_EQ(&pos->name, name)) {
172             return pos;
173         }
174     }
175     return NULL;
176 }
177
178 int
179 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
180 {
181     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
182
183     if (!(twi_node->itype & VFS_INODE_TYPE_DIR)) {
184         return ENOTDIR;
185     }
186
187     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
188     if (child_node) {
189         dnode->inode = child_node->inode;
190         return 0;
191     }
192     return ENOENT;
193 }
194
195 int
196 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
197 {
198     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
199     int counter = 0;
200     struct twifs_node *pos, *n;
201
202     llist_for_each(pos, n, &twi_node->children, siblings)
203     {
204         if (counter++ >= dctx->index) {
205             dctx->index = counter;
206             dctx->read_complete_callback(
207               dctx, pos->name.value, pos->name.len, pos->itype);
208             return 0;
209         }
210     }
211
212     return 1;
213 }
214
215 int
216 __twifs_openfile(struct v_inode* inode, struct v_file* file)
217 {
218     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
219     if (twi_node) {
220         file->inode = twi_node->inode;
221         file->ops = twi_node->fops;
222         return 0;
223     }
224     return ENOTSUP;
225 }