refactor: mount system reworked
[lunaix-os.git] / lunaix-os / includes / lunaix / fs.h
1 #ifndef __LUNAIX_VFS_H
2 #define __LUNAIX_VFS_H
3
4 #include <lunaix/clock.h>
5 #include <lunaix/device.h>
6 #include <lunaix/ds/btrie.h>
7 #include <lunaix/ds/hashtable.h>
8 #include <lunaix/ds/hstr.h>
9 #include <lunaix/ds/llist.h>
10 #include <lunaix/ds/lru.h>
11 #include <lunaix/ds/mutex.h>
12 #include <lunaix/status.h>
13 #include <stdatomic.h>
14
15 #define VFS_NAME_MAXLEN 128
16 #define VFS_MAX_FD 32
17
18 #define VFS_IFDIR 0x1
19 #define VFS_IFFILE 0x2
20 #define VFS_IFSEQDEV 0x4
21 #define VFS_IFVOLDEV 0x8
22 #define VFS_IFSYMLINK 0x16
23
24 #define VFS_WALK_MKPARENT 0x1
25 #define VFS_WALK_FSRELATIVE 0x2
26 #define VFS_WALK_PARENT 0x4
27 #define VFS_WALK_NOFOLLOW 0x4
28
29 #define VFS_HASHTABLE_BITS 10
30 #define VFS_HASHTABLE_SIZE (1 << VFS_HASHTABLE_BITS)
31 #define VFS_HASH_MASK (VFS_HASHTABLE_SIZE - 1)
32 #define VFS_HASHBITS (32 - VFS_HASHTABLE_BITS)
33
34 #define FSTYPE_ROFS 0x1
35
36 #define VFS_VALID_CHAR(chr)                                                    \
37     (('A' <= (chr) && (chr) <= 'Z') || ('a' <= (chr) && (chr) <= 'z') ||       \
38      ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' ||         \
39      (chr) == '-')
40
41 extern struct hstr vfs_ddot;
42 extern struct hstr vfs_dot;
43
44 typedef uint32_t inode_t;
45
46 struct v_dnode;
47 struct v_inode;
48 struct v_superblock;
49 struct v_file;
50 struct v_fd;
51 struct pcache;
52
53 struct filesystem
54 {
55     struct hlist_node fs_list;
56     struct hstr fs_name;
57     uint32_t types;
58     int fs_id; // can be used to detect fs type on partition
59     int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
60     int (*unmount)(struct v_superblock* vsb);
61 };
62
63 struct v_superblock
64 {
65     struct llist_header sb_list;
66     struct device* dev;
67     struct v_dnode* root;
68     struct filesystem* fs;
69     uint32_t iobuf_size;
70     struct hbucket* i_cache;
71     struct
72     {
73         uint32_t (*read_capacity)(struct v_superblock* vsb);
74         uint32_t (*read_usage)(struct v_superblock* vsb);
75     } ops;
76 };
77
78 struct dir_context
79 {
80     int index;
81     void* cb_data;
82     void (*read_complete_callback)(struct dir_context* dctx,
83                                    const char* name,
84                                    const int len,
85                                    const int dtype);
86 };
87
88 struct v_file_ops
89 {
90     int (*write)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
91     int (*read)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
92     int (*readdir)(struct v_inode* inode, struct dir_context* dctx);
93     int (*seek)(struct v_inode* inode, size_t offset);
94     int (*close)(struct v_file* file);
95     int (*sync)(struct v_inode* inode);
96 };
97
98 struct v_file
99 {
100     struct v_inode* inode;
101     struct v_dnode* dnode;
102     struct llist_header* f_list;
103     uint32_t f_pos;
104     atomic_ulong ref_count;
105     struct v_file_ops ops;
106 };
107
108 struct v_fd
109 {
110     struct v_file* file;
111     int flags;
112 };
113
114 struct v_inode
115 {
116     inode_t id;
117     mutex_t lock;
118     uint32_t itype;
119     time_t ctime;
120     time_t mtime;
121     time_t atime;
122     lba_t lb_addr;
123     uint32_t open_count;
124     uint32_t link_count;
125     uint32_t lb_usage;
126     uint32_t fsize;
127     struct hlist_node hash_list;
128     struct lru_node lru;
129     struct pcache* pg_cache;
130     void* data; // 允许底层FS绑定他的一些专有数据
131     struct
132     {
133         int (*create)(struct v_inode* this, struct v_dnode* dnode);
134         int (*open)(struct v_inode* this, struct v_file* file);
135         int (*sync)(struct v_inode* this);
136         int (*mkdir)(struct v_inode* this, struct v_dnode* dnode);
137         int (*rmdir)(struct v_inode* this, struct v_dnode* dir);
138         int (*unlink)(struct v_inode* this);
139         int (*link)(struct v_inode* this, struct v_dnode* new_name);
140         int (*read_symlink)(struct v_inode* this, const char** path_out);
141         int (*set_symlink)(struct v_inode* this, const char* target);
142         int (*dir_lookup)(struct v_inode* this, struct v_dnode* dnode);
143         int (*rename)(struct v_inode* from_inode,
144                       struct v_dnode* from_dnode,
145                       struct v_dnode* to_dnode);
146     } ops;
147     struct v_file_ops default_fops;
148 };
149
150 struct v_mount
151 {
152     mutex_t lock;
153     struct llist_header list;
154     struct llist_header submnts;
155     struct llist_header sibmnts;
156     struct v_mount* parent;
157     struct v_dnode* mnt_point;
158     struct v_superblock* super_block;
159     uint32_t busy_counter;
160 };
161
162 struct v_dnode
163 {
164     mutex_t lock; // sync the path walking
165     struct lru_node lru;
166     struct hstr name;
167     struct v_inode* inode;
168     struct v_dnode* parent;
169     struct hlist_node hash_list;
170     struct llist_header children;
171     struct llist_header siblings;
172     struct v_superblock* super_block;
173     struct v_mount* mnt;
174     atomic_ulong ref_count;
175
176     void* data;
177 };
178
179 struct v_fdtable
180 {
181     struct v_fd* fds[VFS_MAX_FD];
182 };
183
184 struct pcache
185 {
186     struct v_inode* master;
187     struct btrie tree;
188     struct llist_header pages;
189     struct llist_header dirty;
190     uint32_t n_dirty;
191     uint32_t n_pages;
192 };
193
194 struct pcache_pg
195 {
196     struct llist_header pg_list;
197     struct llist_header dirty_list;
198     struct lru_node lru;
199     struct pcache* holder;
200     void* pg;
201     uint32_t flags;
202     uint32_t fpos;
203 };
204 /* --- file system manager --- */
205 void
206 fsm_init();
207
208 void
209 fsm_register(struct filesystem* fs);
210
211 struct filesystem*
212 fsm_get(const char* fs_name);
213
214 void
215 vfs_init();
216
217 struct v_dnode*
218 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
219
220 void
221 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
222
223 int
224 vfs_walk(struct v_dnode* start,
225          const char* path,
226          struct v_dnode** dentry,
227          struct hstr* component,
228          int walk_options);
229
230 int
231 vfs_mount(const char* target, const char* fs_name, struct device* device);
232
233 int
234 vfs_unmount(const char* target);
235
236 int
237 vfs_mount_at(const char* fs_name,
238              struct device* device,
239              struct v_dnode* mnt_point);
240
241 int
242 vfs_unmount_at(struct v_dnode* mnt_point);
243
244 int
245 vfs_mkdir(const char* path, struct v_dnode** dentry);
246
247 int
248 vfs_open(struct v_dnode* dnode, struct v_file** file);
249
250 int
251 vfs_close(struct v_file* file);
252
253 int
254 vfs_fsync(struct v_file* file);
255
256 void
257 vfs_assign_inode(struct v_dnode* assign_to, struct v_inode* inode);
258
259 struct v_superblock*
260 vfs_sb_alloc();
261
262 void
263 vfs_sb_free(struct v_superblock* sb);
264
265 struct v_dnode*
266 vfs_d_alloc();
267
268 void
269 vfs_d_free(struct v_dnode* dnode);
270
271 struct v_inode*
272 vfs_i_alloc(struct v_superblock* sb,
273             uint32_t inode_id,
274             void (*init)(struct v_inode* inode, void* data),
275             void* data);
276
277 void
278 vfs_i_free(struct v_inode* inode);
279
280 int
281 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
282
283 void
284 pcache_init(struct pcache* pcache);
285
286 void
287 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
288
289 struct pcache_pg*
290 pcache_new_page(struct pcache* pcache, uint32_t index);
291
292 void
293 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
294
295 struct pcache_pg*
296 pcache_get_page(struct pcache* pcache,
297                 uint32_t index,
298                 uint32_t* offset,
299                 struct pcache_pg** page);
300
301 int
302 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
303
304 int
305 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
306
307 void
308 pcache_release(struct pcache* pcache);
309
310 int
311 pcache_commit(struct v_inode* inode, struct pcache_pg* page);
312
313 void
314 pcache_commit_all(struct v_inode* inode);
315
316 void
317 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page);
318
319 /**
320  * @brief 将挂载点标记为繁忙
321  *
322  * @param mnt
323  */
324 void
325 mnt_mkbusy(struct v_mount* mnt);
326
327 /**
328  * @brief 将挂载点标记为清闲
329  *
330  * @param mnt
331  */
332 void
333 mnt_chillax(struct v_mount* mnt);
334
335 struct v_mount*
336 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point);
337 #endif /* __LUNAIX_VFS_H */