feat: open(2), close(2), mkdir(2) and readdir(2) syscall
[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 struct v_inode*
32 __twifs_create_inode(struct twifs_node* twi_node);
33
34 int
35 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point);
36
37 void
38 twifs_init()
39 {
40     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
41
42     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
43     twifs->fs_name = HSTR("twifs", 5);
44     twifs->mount = __twifs_mount;
45
46     fsm_register(twifs);
47
48     fs_root = twifs_dir_node(NULL, NULL, 0);
49
50     // 预备一些常用的类别
51     twifs_toplevel_node("kernel", 6);
52     twifs_toplevel_node("dev", 3);
53     twifs_toplevel_node("bus", 3);
54 }
55
56 struct twifs_node*
57 __twifs_new_node(struct twifs_node* parent, const char* name, int name_len)
58 {
59     struct hstr hname = HSTR(name, name_len);
60     hstr_rehash(&hname, HSTR_FULL_HASH);
61
62     struct twifs_node* node = __twifs_get_node(parent, &hname);
63     if (node) {
64         return node;
65     }
66
67     node = cake_grab(twi_pile);
68     memset(node, 0, sizeof(*node));
69
70     node->name = hname;
71     llist_init_head(&node->children);
72
73     if (parent) {
74         llist_append(&parent->children, &node->siblings);
75     }
76
77     return node;
78 }
79
80 struct twifs_node*
81 twifs_file_node(struct twifs_node* parent, const char* name, int name_len)
82 {
83     struct twifs_node* twi_node = __twifs_new_node(parent, name, name_len);
84     twi_node->itype = VFS_INODE_TYPE_FILE;
85
86     struct v_inode* twi_inode = __twifs_create_inode(twi_node);
87     twi_node->inode = twi_inode;
88
89     return twi_inode;
90 }
91
92 struct twifs_node*
93 twifs_dir_node(struct twifs_node* parent, const char* name, int name_len)
94 {
95     struct twifs_node* twi_node = __twifs_new_node(parent, name, name_len);
96     twi_node->itype = VFS_INODE_TYPE_DIR;
97
98     struct v_inode* twi_inode = __twifs_create_inode(twi_node);
99     struct twifs_node* dot = __twifs_new_node(twi_node, ".", 1);
100     struct twifs_node* ddot = __twifs_new_node(twi_node, "..", 2);
101
102     dot->itype = VFS_INODE_TYPE_DIR;
103     ddot->itype = VFS_INODE_TYPE_DIR;
104
105     twi_node->inode = twi_inode;
106     dot->inode = twi_inode;
107     ddot->inode = parent->inode;
108
109     return twi_node;
110 }
111
112 struct twifs_node*
113 twifs_toplevel_node(const char* name, int name_len)
114 {
115     return twifs_dir_node(fs_root, name, name_len);
116 }
117
118 int
119 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
120 {
121     mount_point->inode = fs_root->inode;
122     return 0;
123 }
124
125 struct v_inode*
126 __twifs_create_inode(struct twifs_node* twi_node)
127 {
128     struct v_inode* inode = vfs_i_alloc();
129     *inode = (struct v_inode){ .ctime = 0,
130                                .itype = twi_node->itype,
131                                .lb_addr = 0,
132                                .lb_usage = 0,
133                                .data = twi_node,
134                                .mtime = 0,
135                                .ref_count = 0 };
136     inode->ops.dir_lookup = __twifs_dirlookup;
137     inode->ops.open = __twifs_openfile;
138
139     return inode;
140 }
141
142 struct twifs_node*
143 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
144 {
145     if (!parent)
146         return NULL;
147
148     struct twifs_node *pos, *n;
149     llist_for_each(pos, n, &parent->children, siblings)
150     {
151         if (HSTR_EQ(&pos->name, name)) {
152             return pos;
153         }
154     }
155     return NULL;
156 }
157
158 int
159 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
160 {
161     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
162
163     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
164     if (child_node) {
165         dnode->inode = child_node->inode;
166         return 0;
167     }
168     return ENOENT;
169 }
170
171 int
172 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
173 {
174     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
175     int counter = 0;
176     struct twifs_node *pos, *n;
177
178     llist_for_each(pos, n, &twi_node->children, siblings)
179     {
180         if (counter++ >= dctx->index) {
181             dctx->index = counter;
182             dctx->read_complete_callback(
183               dctx, pos->name.value, pos->name.len, pos->itype);
184             return 1;
185         }
186     }
187
188     return 0;
189 }
190
191 int
192 __twifs_openfile(struct v_inode* inode, struct v_file* file)
193 {
194     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
195     if (twi_node) {
196         file->ops = twi_node->fops;
197         return 0;
198     }
199     return ENOTSUP;
200 }