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/strfmt.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 #include <sys/mm/pagetable.h>
23 static struct twifs_node* fs_root;
25 static struct cake_pile* twi_pile;
27 static volatile u32_t inode_id = 0;
29 extern const struct v_file_ops twifs_file_ops;
30 extern const struct v_inode_ops twifs_inode_ops;
33 __twifs_new_node(struct twifs_node* parent,
38 struct twifs_node* node = cake_grab(twi_pile);
39 memset(node, 0, sizeof(*node));
41 strncpy(node->name_val, name, VFS_NAME_MAXLEN);
43 node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
45 node->ino_id = inode_id++;
46 hstr_rehash(&node->name, HSTR_FULL_HASH);
47 llist_init_head(&node->children);
50 llist_append(&parent->children, &node->siblings);
57 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
59 inode->ops = (struct v_inode_ops*)&twifs_inode_ops;
60 inode->default_fops = (struct v_file_ops*)&twifs_file_ops;
64 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
66 vsb->ops.init_inode = __twifs_init_inode;
68 struct v_inode* inode = vfs_i_alloc(vsb);
73 inode->id = fs_root->ino_id;
74 inode->itype = fs_root->itype;
75 inode->data = fs_root;
78 vfs_assign_inode(mount_point, inode);
83 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
85 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
86 if (!twi_node || !twi_node->ops.write) {
89 return twi_node->ops.write(inode, buffer, len, fpos);
93 __twifs_fwrite_pg(struct v_inode* inode, void* buffer, size_t fpos)
95 return __twifs_fwrite(inode, buffer, PAGE_SIZE, fpos);
99 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
101 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
102 if (!twi_node || !twi_node->ops.read) {
105 return twi_node->ops.read(inode, buffer, len, fpos);
109 __twifs_fread_pg(struct v_inode* inode, void* buffer, size_t fpos)
111 return __twifs_fread(inode, buffer, PAGE_SIZE, fpos);
115 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
120 struct twifs_node *pos, *n;
121 llist_for_each(pos, n, &parent->children, siblings)
123 if (HSTR_EQ(&pos->name, name)) {
131 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
133 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
135 if ((twi_node->itype & F_FILE)) {
139 struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
141 struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
146 if (!(child_inode = vfs_i_alloc(inode->sb))) {
150 child_inode->id = child_node->ino_id;
151 child_inode->itype = child_node->itype;
152 child_inode->data = child_node;
154 vfs_i_addhash(child_inode);
157 dnode->data = child_node->data;
158 vfs_assign_inode(dnode, child_inode);
165 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
167 struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
169 struct twifs_node *pos, *n;
171 llist_for_each(pos, n, &twi_node->children, siblings)
173 if (counter++ >= dctx->index) {
174 dctx->index = counter;
175 dctx->read_complete_callback(
176 dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
185 __twifs_openfile(struct v_inode* inode, struct v_file* file)
187 struct twifs_node* twi_node = (struct twifs_node*)inode->data;
195 twifs_rm_node(struct twifs_node* node)
197 if (!(node->itype & F_FILE) && !llist_empty(&node->children)) {
200 llist_delete(&node->siblings);
201 cake_release(twi_pile, node);
206 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
208 char buf[VFS_NAME_MAXLEN];
209 size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
211 return __twifs_new_node(parent ? parent : fs_root, buf, len, F_FILE);
215 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
220 struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
228 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
233 char buf[VFS_NAME_MAXLEN];
234 size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
235 struct twifs_node* twi_node =
236 __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
246 twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
248 struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
249 twifs->fs_name = HSTR("twifs", 5);
250 twifs->mount = __twifs_mount;
251 twifs->types = FSTYPE_ROFS;
256 fs_root = twifs_dir_node(NULL, NULL, 0, 0);
258 EXPORT_FILE_SYSTEM(twifs, twifs_init);
261 twifs_register_plugins()
263 ldga_invoke_fn0(twiplugin_inits);
267 __twifs_twimap_file_read(struct v_inode* inode,
272 struct twimap* map = twinode_getdata(inode, struct twimap*);
273 return twimap_read(map, buf, len, fpos);
277 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
282 struct twimap* map = twimap_create(data);
283 struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
284 node->ops.read = __twifs_twimap_file_read;
290 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
291 .read = __twifs_fread,
292 .read_page = __twifs_fread_pg,
293 .write = __twifs_fwrite,
294 .write_page = __twifs_fwrite_pg,
295 .readdir = __twifs_iterate_dir };
297 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
298 .mkdir = default_inode_mkdir,
299 .rmdir = default_inode_rmdir,
300 .open = __twifs_openfile };