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/fs/twimap.h>
17 #include <lunaix/mm/cake.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/spike.h>
21 static struct twifs_node* fs_root;
23 static struct cake_pile* twi_pile;
25 static volatile uint32_t inode_id = 0;
27 extern const struct v_file_ops twifs_file_ops;
28 extern const struct v_inode_ops twifs_inode_ops;
31 __twifs_new_node(struct twifs_node* parent,
36 struct twifs_node* node = cake_grab(twi_pile);
37 memset(node, 0, sizeof(*node));
39 strncpy(node->name_val, name, VFS_NAME_MAXLEN);
41 node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
43 node->ino_id = inode_id++;
44 hstr_rehash(&node->name, HSTR_FULL_HASH);
45 llist_init_head(&node->children);
48 llist_append(&parent->children, &node->siblings);
55 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
57 inode->ops = &twifs_inode_ops;
58 inode->default_fops = &twifs_file_ops;
62 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
65 vsb->ops.init_inode = __twifs_init_inode;
67 struct v_inode* inode = vfs_i_alloc(vsb);
72 inode->id = fs_root->ino_id;
73 inode->itype = fs_root->itype;
74 inode->data = fs_root;
77 vfs_assign_inode(mount_point, inode);
82 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
84 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
85 if (!twi_node || !twi_node->ops.write) {
88 return twi_node->ops.write(inode, buffer, len, fpos);
92 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
94 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
95 if (!twi_node || !twi_node->ops.read) {
98 return twi_node->ops.read(inode, buffer, len, fpos);
102 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
107 struct twifs_node *pos, *n;
108 llist_for_each(pos, n, &parent->children, siblings)
110 if (HSTR_EQ(&pos->name, name)) {
118 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
120 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
122 if (!(twi_node->itype & VFS_IFDIR)) {
126 struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
128 struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
133 if (!(child_inode = vfs_i_alloc(inode->sb))) {
137 child_inode->id = child_node->ino_id;
138 child_inode->itype = child_node->itype;
139 child_inode->data = child_node;
141 vfs_i_addhash(child_inode);
144 dnode->data = child_node->data;
145 vfs_assign_inode(dnode, child_inode);
152 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
154 struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
156 struct twifs_node *pos, *n;
158 llist_for_each(pos, n, &twi_node->children, siblings)
160 if (counter++ >= dctx->index) {
161 dctx->index = counter;
162 dctx->read_complete_callback(
163 dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
172 __twifs_openfile(struct v_inode* inode, struct v_file* file)
174 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
182 twifs_rm_node(struct twifs_node* node)
184 if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
187 llist_delete(&node->siblings);
188 cake_release(twi_pile, node);
193 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
195 char buf[VFS_NAME_MAXLEN];
196 size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
198 return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV);
202 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
207 struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
215 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
220 char buf[VFS_NAME_MAXLEN];
221 size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
222 struct twifs_node* twi_node =
223 __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
233 twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
235 struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
236 twifs->fs_name = HSTR("twifs", 5);
237 twifs->mount = __twifs_mount;
238 twifs->types = FSTYPE_ROFS;
243 fs_root = twifs_dir_node(NULL, NULL, 0, 0);
247 __twifs_twimap_file_read(struct v_inode* inode,
252 struct twimap* map = twinode_getdata(inode, struct twimap*);
253 return twimap_read(map, buf, len, fpos);
257 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
262 struct twimap* map = twimap_create(data);
263 struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
264 node->ops.read = __twifs_twimap_file_read;
270 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
271 .read = __twifs_fread,
272 .write = __twifs_fwrite,
273 .readdir = __twifs_iterate_dir };
275 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
276 .mkdir = default_inode_mkdir,
277 .rmdir = default_inode_rmdir,
278 .open = __twifs_openfile };