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