feat: ability to evict dnode and inode cache
[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 FSTYPE_ROFS 0x1
30
31 #define VFS_VALID_CHAR(chr)                                                    \
32     (('A' <= (chr) && (chr) <= 'Z') || ('a' <= (chr) && (chr) <= 'z') ||       \
33      ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' ||         \
34      (chr) == '-')
35
36 extern struct hstr vfs_ddot;
37 extern struct hstr vfs_dot;
38
39 struct v_dnode;
40 struct v_inode;
41 struct v_superblock;
42 struct v_file;
43 struct v_fd;
44 struct pcache;
45
46 struct filesystem
47 {
48     struct hlist_node fs_list;
49     struct hstr fs_name;
50     uint32_t types;
51     int fs_id; // can be used to detect fs type on partition
52     int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
53     int (*unmount)(struct v_superblock* vsb);
54 };
55
56 struct v_superblock
57 {
58     struct llist_header sb_list;
59     int fs_id;
60     struct device* dev;
61     struct v_dnode* root;
62     struct filesystem* fs;
63     uint32_t iobuf_size;
64     struct
65     {
66         uint32_t (*read_capacity)(struct v_superblock* vsb);
67         uint32_t (*read_usage)(struct v_superblock* vsb);
68     } ops;
69 };
70
71 struct dir_context
72 {
73     int index;
74     void* cb_data;
75     void (*read_complete_callback)(struct dir_context* dctx,
76                                    const char* name,
77                                    const int len,
78                                    const int dtype);
79 };
80
81 struct v_file_ops
82 {
83     int (*write)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
84     int (*read)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
85     int (*readdir)(struct v_inode* inode, struct dir_context* dctx);
86     int (*seek)(struct v_inode* inode, size_t offset);
87     int (*close)(struct v_file* file);
88     int (*sync)(struct v_inode* inode);
89 };
90
91 struct v_file
92 {
93     struct v_inode* inode;
94     struct v_dnode* dnode;
95     struct llist_header* f_list;
96     uint32_t f_pos;
97     atomic_ulong ref_count;
98     struct v_file_ops ops;
99 };
100
101 struct v_fd
102 {
103     struct v_file* file;
104     int flags;
105 };
106
107 struct v_inode
108 {
109     uint32_t id;
110     mutex_t lock;
111     uint32_t itype;
112     time_t ctime;
113     time_t mtime;
114     time_t atime;
115     lba_t lb_addr;
116     uint32_t open_count;
117     uint32_t link_count;
118     uint32_t lb_usage;
119     uint32_t fsize;
120     struct hlist_node hash_list;
121     struct lru_node lru;
122     struct pcache* pg_cache;
123     void* data; // 允许底层FS绑定他的一些专有数据
124     struct
125     {
126         int (*create)(struct v_inode* this, struct v_dnode* dnode);
127         int (*open)(struct v_inode* this, struct v_file* file);
128         int (*sync)(struct v_inode* this);
129         int (*mkdir)(struct v_inode* this, struct v_dnode* dnode);
130         int (*rmdir)(struct v_inode* this, struct v_dnode* dir);
131         int (*unlink)(struct v_inode* this);
132         int (*link)(struct v_inode* this, struct v_dnode* new_name);
133         int (*read_symlink)(struct v_inode* this, const char** path_out);
134         int (*set_symlink)(struct v_inode* this, const char* target);
135         int (*dir_lookup)(struct v_inode* this, struct v_dnode* dnode);
136         int (*rename)(struct v_inode* from_inode,
137                       struct v_dnode* from_dnode,
138                       struct v_dnode* to_dnode);
139     } ops;
140     struct v_file_ops default_fops;
141 };
142
143 struct v_dnode
144 {
145     mutex_t lock; // sync the path walking
146     struct lru_node lru;
147     struct hstr name;
148     struct v_inode* inode;
149     struct v_dnode* parent;
150     struct hlist_node hash_list;
151     struct llist_header children;
152     struct llist_header siblings;
153     struct v_superblock* super_block;
154     atomic_ulong ref_count;
155 };
156
157 struct v_fdtable
158 {
159     struct v_fd* fds[VFS_MAX_FD];
160 };
161
162 struct pcache
163 {
164     struct v_inode* master;
165     struct btrie tree;
166     struct llist_header pages;
167     struct llist_header dirty;
168     uint32_t n_dirty;
169     uint32_t n_pages;
170 };
171
172 struct pcache_pg
173 {
174     struct llist_header pg_list;
175     struct llist_header dirty_list;
176     struct lru_node lru;
177     struct pcache* holder;
178     void* pg;
179     uint32_t flags;
180     uint32_t fpos;
181 };
182 /* --- file system manager --- */
183 void
184 fsm_init();
185
186 void
187 fsm_register(struct filesystem* fs);
188
189 struct filesystem*
190 fsm_get(const char* fs_name);
191
192 void
193 vfs_init();
194
195 struct v_dnode*
196 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
197
198 void
199 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
200
201 int
202 vfs_walk(struct v_dnode* start,
203          const char* path,
204          struct v_dnode** dentry,
205          struct hstr* component,
206          int walk_options);
207
208 int
209 vfs_mount(const char* target, const char* fs_name, struct device* device);
210
211 int
212 vfs_unmount(const char* target);
213
214 int
215 vfs_mount_at(const char* fs_name,
216              struct device* device,
217              struct v_dnode* mnt_point);
218
219 int
220 vfs_unmount_at(struct v_dnode* mnt_point);
221
222 int
223 vfs_mkdir(const char* path, struct v_dnode** dentry);
224
225 int
226 vfs_open(struct v_dnode* dnode, struct v_file** file);
227
228 int
229 vfs_close(struct v_file* file);
230
231 int
232 vfs_fsync(struct v_file* file);
233
234 void
235 vfs_assign_inode(struct v_dnode* assign_to, struct v_inode* inode);
236
237 struct v_superblock*
238 vfs_sb_alloc();
239
240 void
241 vfs_sb_free(struct v_superblock* sb);
242
243 struct v_dnode*
244 vfs_d_alloc();
245
246 void
247 vfs_d_free(struct v_dnode* dnode);
248
249 struct v_inode*
250 vfs_i_alloc(dev_t device_id, uint32_t inode_id);
251
252 void
253 vfs_i_free(struct v_inode* inode);
254
255 int
256 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
257
258 void
259 pcache_init(struct pcache* pcache);
260
261 void
262 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
263
264 struct pcache_pg*
265 pcache_new_page(struct pcache* pcache, uint32_t index);
266
267 void
268 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
269
270 struct pcache_pg*
271 pcache_get_page(struct pcache* pcache,
272                 uint32_t index,
273                 uint32_t* offset,
274                 struct pcache_pg** page);
275
276 int
277 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
278
279 int
280 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
281
282 void
283 pcache_release(struct pcache* pcache);
284
285 int
286 pcache_commit(struct v_inode* inode, struct pcache_pg* page);
287
288 void
289 pcache_commit_all(struct v_inode* inode);
290
291 void
292 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page);
293 #endif /* __LUNAIX_VFS_H */