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 uint32_t inode_id = 0;
24 extern const struct v_file_ops twifs_file_ops;
25 extern const struct v_inode_ops twifs_inode_ops;
28 __twifs_new_node(struct twifs_node* parent,
33 struct twifs_node* node = cake_grab(twi_pile);
34 memset(node, 0, sizeof(*node));
36 node->name = HSTR(name, name_len);
38 node->ino_id = inode_id++;
39 hstr_rehash(&node->name, HSTR_FULL_HASH);
40 llist_init_head(&node->children);
43 llist_append(&parent->children, &node->siblings);
50 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
52 inode->ops = &twifs_inode_ops;
53 inode->default_fops = &twifs_file_ops;
57 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
60 vsb->ops.init_inode = __twifs_init_inode;
62 struct v_inode* inode = vfs_i_alloc(vsb);
67 inode->id = fs_root->ino_id;
68 inode->itype = fs_root->itype;
69 inode->data = fs_root;
72 vfs_assign_inode(mount_point, inode);
77 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
79 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
80 if (!twi_node || !twi_node->ops.write) {
83 return twi_node->ops.write(inode, buffer, len, fpos);
87 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
89 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
90 if (!twi_node || !twi_node->ops.read) {
93 return twi_node->ops.read(inode, buffer, len, fpos);
97 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
102 struct twifs_node *pos, *n;
103 llist_for_each(pos, n, &parent->children, siblings)
105 if (HSTR_EQ(&pos->name, name)) {
113 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
115 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
117 if (!(twi_node->itype & VFS_IFDIR)) {
121 struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
123 struct v_inode* inode = vfs_i_find(inode->sb, child_node->ino_id);
128 if (!(inode = vfs_i_alloc(inode->sb))) {
132 inode->id = child_node->ino_id;
133 inode->itype = child_node->itype;
134 inode->data = child_node;
136 vfs_i_addhash(inode);
139 dnode->data = child_node->data;
140 vfs_assign_inode(dnode, inode);
147 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
149 struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
151 struct twifs_node *pos, *n;
153 llist_for_each(pos, n, &twi_node->children, siblings)
155 if (counter++ >= dctx->index) {
156 dctx->index = counter;
157 dctx->read_complete_callback(
158 dctx, pos->name.value, pos->name.len, pos->itype);
167 __twifs_openfile(struct v_inode* inode, struct v_file* file)
169 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
177 twifs_rm_node(struct twifs_node* node)
179 if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
182 llist_delete(&node->siblings);
183 cake_release(twi_pile, node);
188 twifs_file_node(struct twifs_node* parent,
193 struct twifs_node* twi_node =
194 __twifs_new_node(parent, name, name_len, VFS_IFFILE | itype);
200 twifs_dir_node(struct twifs_node* parent,
205 struct hstr hname = HSTR(name, name_len);
206 hstr_rehash(&hname, HSTR_FULL_HASH);
207 struct twifs_node* node = __twifs_get_node(parent, &hname);
212 struct twifs_node* twi_node =
213 __twifs_new_node(parent, name, name_len, VFS_IFDIR | itype);
219 twifs_toplevel_node(const char* name, int name_len, uint32_t itype)
221 return twifs_dir_node(fs_root, name, name_len, itype);
227 twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
229 struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
230 twifs->fs_name = HSTR("twifs", 5);
231 twifs->mount = __twifs_mount;
232 twifs->types = FSTYPE_ROFS;
237 fs_root = twifs_dir_node(NULL, NULL, 0, 0);
240 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
241 .read = __twifs_fread,
242 .write = __twifs_fwrite,
243 .readdir = __twifs_iterate_dir };
245 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
246 .mkdir = default_inode_mkdir,
247 .rmdir = default_inode_rmdir,
248 .open = __twifs_openfile };