3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief TwiFS - A pseudo file system for kernel state exposure.
8 * @copyright Copyright (c) 2022
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>
18 static struct twifs_node* fs_root;
20 static struct cake_pile* twi_pile;
22 static volatile int32_t inode_id = 0;
25 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode);
28 __twifs_openfile(struct v_inode* inode, struct v_file* file);
31 __twifs_get_node(struct twifs_node* parent, struct hstr* name);
34 __twifs_create_inode(struct twifs_node* twi_node);
37 __twifs_iterate_dir(struct v_inode* inode, struct dir_context* dctx);
40 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point);
43 __twifs_mkdir(struct v_inode* inode, struct v_dnode* dnode);
46 __twifs_rmstuff(struct v_inode* inode);
49 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
52 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
57 twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
59 struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
60 twifs->fs_name = HSTR("twifs", 5);
61 twifs->mount = __twifs_mount;
62 twifs->types = FSTYPE_ROFS;
67 fs_root = twifs_dir_node(NULL, NULL, 0, 0);
71 __twifs_new_node(struct twifs_node* parent,
76 struct twifs_node* node = cake_grab(twi_pile);
77 memset(node, 0, sizeof(*node));
79 node->name = HSTR(name, name_len);
81 hstr_rehash(&node->name, HSTR_FULL_HASH);
82 llist_init_head(&node->children);
85 llist_append(&parent->children, &node->siblings);
92 twifs_rm_node(struct twifs_node* node)
94 if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
97 llist_delete(&node->siblings);
98 vfs_i_free(node->inode);
99 cake_release(twi_pile, node);
104 twifs_file_node(struct twifs_node* parent,
109 struct twifs_node* twi_node =
110 __twifs_new_node(parent, name, name_len, VFS_IFFILE | itype);
112 struct v_inode* twi_inode = __twifs_create_inode(twi_node);
113 twi_node->inode = twi_inode;
119 twifs_dir_node(struct twifs_node* parent,
124 struct hstr hname = HSTR(name, name_len);
125 hstr_rehash(&hname, HSTR_FULL_HASH);
126 struct twifs_node* node = __twifs_get_node(parent, &hname);
131 struct twifs_node* twi_node =
132 __twifs_new_node(parent, name, name_len, VFS_IFDIR | itype);
134 struct v_inode* twi_inode = __twifs_create_inode(twi_node);
135 twi_node->inode = twi_inode;
141 twifs_toplevel_node(const char* name, int name_len, uint32_t itype)
143 return twifs_dir_node(fs_root, name, name_len, itype);
147 __twifs_mkdir(struct v_inode* inode, struct v_dnode* dnode)
149 struct twifs_node* parent_node = (struct twifs_node*)inode->data;
150 if (!(parent_node->itype & VFS_IFDIR)) {
153 struct twifs_node* new_node =
154 twifs_dir_node(parent_node, dnode->name.value, dnode->name.len, 0);
155 dnode->inode = new_node->inode;
161 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
163 mount_point->inode = fs_root->inode;
168 __twifs_create_inode(struct twifs_node* twi_node)
170 struct v_inode* inode = vfs_i_alloc(1, inode_id++);
171 inode->itype = twi_node->itype;
172 inode->data = twi_node;
174 inode->ctime = clock_unixtime();
175 inode->atime = inode->ctime;
176 inode->mtime = inode->ctime;
178 inode->ops.dir_lookup = __twifs_dirlookup;
179 inode->ops.mkdir = __twifs_mkdir;
180 inode->ops.unlink = __twifs_rmstuff;
181 inode->ops.rmdir = __twifs_rmstuff;
182 inode->ops.open = __twifs_openfile;
184 inode->default_fops = (struct v_file_ops){ .read = __twifs_fread,
185 .write = __twifs_fwrite,
186 .readdir = __twifs_iterate_dir };
192 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
194 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
195 if (!twi_node || !twi_node->ops.write) {
198 return twi_node->ops.write(inode, buffer, len, fpos);
202 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
204 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
205 if (!twi_node || !twi_node->ops.read) {
208 return twi_node->ops.read(inode, buffer, len, fpos);
212 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
217 struct twifs_node *pos, *n;
218 llist_for_each(pos, n, &parent->children, siblings)
220 if (HSTR_EQ(&pos->name, name)) {
228 __twifs_rmstuff(struct v_inode* inode)
230 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
231 return twifs_rm_node(twi_node);
235 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
237 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
239 if (!(twi_node->itype & VFS_IFDIR)) {
243 struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
245 dnode->inode = child_node->inode;
252 __twifs_iterate_dir(struct v_inode* inode, struct dir_context* dctx)
254 struct twifs_node* twi_node = (struct twifs_node*)(inode->data);
256 struct twifs_node *pos, *n;
258 llist_for_each(pos, n, &twi_node->children, siblings)
260 if (counter++ >= dctx->index) {
261 dctx->index = counter;
262 dctx->read_complete_callback(
263 dctx, pos->name.value, pos->name.len, pos->itype);
272 __twifs_openfile(struct v_inode* inode, struct v_file* file)
274 struct twifs_node* twi_node = (struct twifs_node*)inode->data;