feat: twifs - pseudo file system for lunaix kernel state exposure
[lunaix-os.git] / lunaix-os / kernel / fs / twifs.c
1 /**
2  * @file twifs.c
3  * @author Lunaixsky (zelong56@gmail.com)
4  * @brief TwiFS - A pseudo file system for kernel state exposure.
5  * @version 0.1
6  * @date 2022-07-21
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
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>
17
18 static struct twifs_node fs_root;
19
20 static struct cake_pile* twi_pile;
21
22 int
23 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode);
24
25 int
26 __twifs_openfile(struct v_inode* inode, struct v_file* file);
27
28 struct twifs_node*
29 __twifs_get_node(struct twifs_node* parent, struct hstr* name);
30
31 void
32 twifs_init()
33 {
34     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
35
36     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
37     twifs->fs_name = HSTR("twifs", 5);
38     twifs->mount = __twifs_mount;
39
40     fsm_register(twifs);
41
42     memset(&fs_root, 0, sizeof(fs_root));
43     llist_init_head(&fs_root.children);
44
45     // 预备一些常用的类别
46     twifs_toplevel_node("kernel", 6);
47     twifs_toplevel_node("dev", 3);
48     twifs_toplevel_node("bus", 3);
49 }
50
51 struct twifs_node*
52 twifs_child_node(struct twifs_node* parent, const char* name, int name_len)
53 {
54     struct hstr hname = HSTR(name, name_len);
55     hstr_rehash(&hname, HSTR_FULL_HASH);
56
57     struct twifs_node* node = __twifs_get_node(parent, &hname);
58     if (node) {
59         return node;
60     }
61
62     node = cake_grab(twi_pile);
63     memset(node, 0, sizeof(*node));
64
65     node->name = hname;
66     llist_init_head(&node->children);
67     llist_append(&parent->children, &node->siblings);
68
69     return node;
70 }
71
72 struct twifs_node*
73 twifs_toplevel_node(const char* name, int name_len)
74 {
75     return twifs_child_node(&fs_root, name, name_len);
76 }
77
78 void
79 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
80 {
81     mount_point->inode = __twifs_create_inode(&fs_root);
82 }
83
84 struct v_inode*
85 __twifs_create_inode(struct twifs_node* twi_node)
86 {
87     struct v_inode* inode = vfs_i_alloc();
88     *inode = (struct v_inode){ .ctime = 0,
89                                .itype = twi_node->itype,
90                                .lb_addr = 0,
91                                .lb_usage = 0,
92                                .data = twi_node,
93                                .mtime = 0,
94                                .ref_count = 0 };
95     inode->ops.dir_lookup = __twifs_dirlookup;
96     inode->ops.open = __twifs_openfile;
97 }
98
99 struct twifs_node*
100 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
101 {
102     struct twifs_node *pos, *n;
103     llist_for_each(pos, n, &parent->children, siblings)
104     {
105         if (HSTR_EQ(&pos->name, name)) {
106             return pos;
107         }
108     }
109     return NULL;
110 }
111
112 int
113 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
114 {
115     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
116
117     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
118     if (child_node) {
119         dnode->inode = __twifs_create_inode(child_node);
120         return 0;
121     }
122     return VFS_ENODIR;
123 }
124
125 int
126 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
127 {
128     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
129     int counter = 0;
130     struct twifs_node *pos, *n;
131
132     llist_for_each(pos, n, &twi_node->children, siblings)
133     {
134         if (counter++ >= dctx->index) {
135             dctx->index = counter;
136             dctx->read_complete_callback(dctx, pos->name.value, pos->itype);
137             return 0;
138         }
139     }
140
141     return VFS_EENDOFDIR;
142 }
143
144 int
145 __twifs_openfile(struct v_inode* inode, struct v_file* file)
146 {
147     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
148     if (twi_node) {
149         file->ops = twi_node->fops;
150         return 1;
151     }
152     return 0;
153 }