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/stdio.h>
12 #include <klibc/string.h>
13 #include <lunaix/clock.h>
14 #include <lunaix/fs.h>
15 #include <lunaix/fs/twifs.h>
16 #include <lunaix/mm/cake.h>
17 #include <lunaix/mm/valloc.h>
18 #include <lunaix/spike.h>
20 static struct twifs_node* fs_root;
22 static struct cake_pile* twi_pile;
24 static volatile uint32_t inode_id = 0;
26 extern const struct v_file_ops twifs_file_ops;
27 extern const struct v_inode_ops twifs_inode_ops;
30 __twifs_new_node(struct twifs_node* parent,
35 struct twifs_node* node = cake_grab(twi_pile);
36 memset(node, 0, sizeof(*node));
38 strncpy(node->name_val, name, VFS_NAME_MAXLEN);
40 node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
42 node->ino_id = inode_id++;
43 hstr_rehash(&node->name, HSTR_FULL_HASH);
44 llist_init_head(&node->children);
47 llist_append(&parent->children, &node->siblings);
54 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
56 inode->ops = &twifs_inode_ops;
57 inode->default_fops = &twifs_file_ops;
61 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
64 vsb->ops.init_inode = __twifs_init_inode;
66 struct v_inode* inode = vfs_i_alloc(vsb);
71 inode->id = fs_root->ino_id;
72 inode->itype = fs_root->itype;
73 inode->data = fs_root;
76 vfs_assign_inode(mount_point, inode);
81 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
83 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
84 if (!twi_node || !twi_node->ops.write) {
87 return twi_node->ops.write(inode, buffer, len, fpos);
91 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
93 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
94 if (!twi_node || !twi_node->ops.read) {
97 return twi_node->ops.read(inode, buffer, len, fpos);
101 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
106 struct twifs_node *pos, *n;
107 llist_for_each(pos, n, &parent->children, siblings)
109 if (HSTR_EQ(&pos->name, name)) {
117 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
119 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
121 if (!(twi_node->itype & VFS_IFDIR)) {
125 struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
127 struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
132 if (!(child_inode = vfs_i_alloc(inode->sb))) {
136 child_inode->id = child_node->ino_id;
137 child_inode->itype = child_node->itype;
138 child_inode->data = child_node;
140 vfs_i_addhash(child_inode);
143 dnode->data = child_node->data;
144 vfs_assign_inode(dnode, child_inode);
151 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
153 struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
155 struct twifs_node *pos, *n;
157 llist_for_each(pos, n, &twi_node->children, siblings)
159 if (counter++ >= dctx->index) {
160 dctx->index = counter;
161 dctx->read_complete_callback(
162 dctx, pos->name.value, pos->name.len, pos->itype);
171 __twifs_openfile(struct v_inode* inode, struct v_file* file)
173 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
181 twifs_rm_node(struct twifs_node* node)
183 if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
186 llist_delete(&node->siblings);
187 cake_release(twi_pile, node);
192 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
194 char buf[VFS_NAME_MAXLEN];
195 size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
197 return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV);
201 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
206 struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
214 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
219 char buf[VFS_NAME_MAXLEN];
220 size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
221 struct twifs_node* twi_node =
222 __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
232 twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
234 struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
235 twifs->fs_name = HSTR("twifs", 5);
236 twifs->mount = __twifs_mount;
237 twifs->types = FSTYPE_ROFS;
242 fs_root = twifs_dir_node(NULL, NULL, 0, 0);
245 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
246 .read = __twifs_fread,
247 .write = __twifs_fwrite,
248 .readdir = __twifs_iterate_dir };
250 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
251 .mkdir = default_inode_mkdir,
252 .rmdir = default_inode_rmdir,
253 .open = __twifs_openfile };