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