refactor: kernel space yield() for controllable, flexible task switching
[lunaix-os.git] / lunaix-os / includes / lunaix / fs.h
1 #ifndef __LUNAIX_VFS_H
2 #define __LUNAIX_VFS_H
3
4 #include <hal/ahci/hba.h>
5 #include <lunaix/block.h>
6 #include <lunaix/clock.h>
7 #include <lunaix/ds/btrie.h>
8 #include <lunaix/ds/hashtable.h>
9 #include <lunaix/ds/hstr.h>
10 #include <lunaix/ds/llist.h>
11 #include <lunaix/status.h>
12
13 #define VFS_NAME_MAXLEN 128
14 #define VFS_MAX_FD 32
15
16 #define VFS_IFDIR 0x1
17 #define VFS_IFFILE 0x2
18 #define VFS_IFSEQDEV 0x4
19 #define VFS_IFVOLDEV 0x8
20 #define VFS_IFSYMLINK 0x16
21
22 #define VFS_WALK_MKPARENT 0x1
23 #define VFS_WALK_FSRELATIVE 0x2
24 #define VFS_WALK_PARENT 0x4
25 #define VFS_WALK_NOFOLLOW 0x4
26
27 #define FSTYPE_ROFS 0x1
28
29 #define VFS_VALID_CHAR(chr)                                                    \
30     ('A' <= (chr) && (chr) <= 'Z' || 'a' <= (chr) && (chr) <= 'z' ||           \
31      '0' <= (chr) && (chr) <= '9' || (chr) == '.' || (chr) == '_' ||           \
32      (chr) == '-')
33
34 extern struct hstr vfs_ddot;
35 extern struct hstr vfs_dot;
36
37 struct v_dnode;
38 struct pcache;
39
40 struct filesystem
41 {
42     struct hlist_node fs_list;
43     struct hstr fs_name;
44     uint32_t types;
45     int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
46     int (*unmount)(struct v_superblock* vsb);
47 };
48
49 struct v_superblock
50 {
51     struct llist_header sb_list;
52     int fs_id;
53     bdev_t dev;
54     struct v_dnode* root;
55     struct filesystem* fs;
56     uint32_t iobuf_size;
57     struct
58     {
59         uint32_t (*read_capacity)(struct v_superblock* vsb);
60         uint32_t (*read_usage)(struct v_superblock* vsb);
61     } ops;
62 };
63
64 struct dir_context
65 {
66     int index;
67     void* cb_data;
68     void (*read_complete_callback)(struct dir_context* dctx,
69                                    const char* name,
70                                    const int len,
71                                    const int dtype);
72 };
73
74 struct v_file_ops
75 {
76     int (*write)(struct v_file* file, void* buffer, size_t len, size_t fpos);
77     int (*read)(struct v_file* file, void* buffer, size_t len, size_t fpos);
78     int (*readdir)(struct v_file* file, struct dir_context* dctx);
79     int (*seek)(struct v_file* file, size_t offset);
80     int (*rename)(struct v_file* file, char* new_name);
81     int (*close)(struct v_file* file);
82     int (*sync)(struct v_file* file);
83 };
84
85 struct v_file
86 {
87     struct v_inode* inode;
88     struct v_dnode* dnode;
89     struct llist_header* f_list;
90     uint32_t f_pos;
91     uint32_t ref_count;
92     void* data; // 允许底层FS绑定他的一些专有数据
93     struct v_file_ops ops;
94 };
95
96 struct v_fd
97 {
98     struct v_file* file;
99     int flags;
100 };
101
102 struct v_inode
103 {
104     uint32_t itype;
105     time_t ctime;
106     time_t mtime;
107     time_t atime;
108     lba_t lb_addr;
109     uint32_t open_count;
110     uint32_t link_count;
111     uint32_t lb_usage;
112     uint32_t fsize;
113     struct pcache* pg_cache;
114     void* data; // 允许底层FS绑定他的一些专有数据
115     struct
116     {
117         int (*create)(struct v_inode* this);
118         int (*open)(struct v_inode* this, struct v_file* file);
119         int (*sync)(struct v_inode* this);
120         int (*mkdir)(struct v_inode* this, struct v_dnode* dnode);
121         int (*rmdir)(struct v_inode* this);
122         int (*unlink)(struct v_inode* this);
123         int (*link)(struct v_inode* this, struct v_dnode* new_name);
124         int (*read_symlink)(struct v_inode* this, const char** path_out);
125         int (*symlink)(struct v_inode* this, const char* target);
126         int (*dir_lookup)(struct v_inode* this, struct v_dnode* dnode);
127     } ops;
128     struct v_file_ops default_fops;
129 };
130
131 struct v_dnode
132 {
133     struct hstr name;
134     struct v_inode* inode;
135     struct v_dnode* parent;
136     struct hlist_node hash_list;
137     struct llist_header children;
138     struct llist_header siblings;
139     struct v_superblock* super_block;
140     uint32_t ref_count;
141     struct
142     {
143         void (*destruct)(struct v_dnode* dnode);
144     } ops;
145 };
146
147 struct v_fdtable
148 {
149     struct v_fd* fds[VFS_MAX_FD];
150 };
151
152 struct pcache_pg
153 {
154     struct llist_header pg_list;
155     struct llist_header dirty_list;
156     void* pg;
157     uint32_t flags;
158     uint32_t fpos;
159 };
160
161 struct pcache
162 {
163     struct btrie tree;
164     struct llist_header pages;
165     struct llist_header dirty;
166     uint32_t n_dirty;
167     uint32_t n_pages;
168 };
169
170 /* --- file system manager --- */
171 void
172 fsm_init();
173
174 void
175 fsm_register(struct filesystem* fs);
176
177 struct filesystem*
178 fsm_get(const char* fs_name);
179
180 void
181 vfs_init();
182
183 struct v_dnode*
184 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
185
186 void
187 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
188
189 int
190 vfs_walk(struct v_dnode* start,
191          const char* path,
192          struct v_dnode** dentry,
193          struct hstr* component,
194          int walk_options);
195
196 int
197 vfs_mount(const char* target, const char* fs_name, bdev_t device);
198
199 int
200 vfs_unmount(const char* target);
201
202 int
203 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point);
204
205 int
206 vfs_unmount_at(struct v_dnode* mnt_point);
207
208 int
209 vfs_mkdir(const char* path, struct v_dnode** dentry);
210
211 int
212 vfs_open(struct v_dnode* dnode, struct v_file** file);
213
214 int
215 vfs_close(struct v_file* file);
216
217 int
218 vfs_fsync(struct v_file* file);
219
220 struct v_superblock*
221 vfs_sb_alloc();
222
223 void
224 vfs_sb_free(struct v_superblock* sb);
225
226 struct v_dnode*
227 vfs_d_alloc();
228
229 void
230 vfs_d_free(struct v_dnode* dnode);
231
232 struct v_inode*
233 vfs_i_alloc();
234
235 void
236 vfs_i_free(struct v_inode* inode);
237
238 int
239 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
240
241 void
242 pcache_init(struct pcache* pcache);
243
244 void
245 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
246
247 struct pcache_pg*
248 pcache_new_page(struct pcache* pcache, uint32_t index);
249
250 void
251 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
252
253 struct pcache_pg*
254 pcache_get_page(struct pcache* pcache,
255                 uint32_t index,
256                 uint32_t* offset,
257                 struct pcache_pg** page);
258
259 int
260 pcache_write(struct v_file* file, void* data, uint32_t len, uint32_t fpos);
261
262 int
263 pcache_read(struct v_file* file, void* data, uint32_t len, uint32_t fpos);
264
265 void
266 pcache_release(struct pcache* pcache);
267
268 int
269 pcache_commit(struct v_file* file, struct pcache_pg* page);
270
271 void
272 pcache_invalidate(struct v_file* file, struct pcache_pg* page);
273
274 void
275 pcache_commit_all(struct v_file* file);
276 #endif /* __LUNAIX_VFS_H */