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