Merge branch 'master' into 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/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     struct
141     {
142         void (*destruct)(struct v_dnode* dnode);
143     } ops;
144 };
145
146 struct v_fdtable
147 {
148     struct v_fd* fds[VFS_MAX_FD];
149 };
150
151 struct pcache_pg
152 {
153     struct llist_header pg_list;
154     struct llist_header dirty_list;
155     void* pg;
156     uint32_t flags;
157     uint32_t fpos;
158 };
159
160 struct pcache
161 {
162     struct btrie tree;
163     struct llist_header pages;
164     struct llist_header dirty;
165     uint32_t n_dirty;
166     uint32_t n_pages;
167 };
168
169 /* --- file system manager --- */
170 void
171 fsm_init();
172
173 void
174 fsm_register(struct filesystem* fs);
175
176 struct filesystem*
177 fsm_get(const char* fs_name);
178
179 void
180 vfs_init();
181
182 struct v_dnode*
183 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
184
185 void
186 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
187
188 int
189 vfs_walk(struct v_dnode* start,
190          const char* path,
191          struct v_dnode** dentry,
192          struct hstr* component,
193          int walk_options);
194
195 int
196 vfs_mount(const char* target, const char* fs_name, bdev_t device);
197
198 int
199 vfs_unmount(const char* target);
200
201 int
202 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point);
203
204 int
205 vfs_unmount_at(struct v_dnode* mnt_point);
206
207 int
208 vfs_mkdir(const char* path, struct v_dnode** dentry);
209
210 int
211 vfs_open(struct v_dnode* dnode, struct v_file** file);
212
213 int
214 vfs_close(struct v_file* file);
215
216 int
217 vfs_fsync(struct v_file* file);
218
219 struct v_superblock*
220 vfs_sb_alloc();
221
222 void
223 vfs_sb_free(struct v_superblock* sb);
224
225 struct v_dnode*
226 vfs_d_alloc();
227
228 void
229 vfs_d_free(struct v_dnode* dnode);
230
231 struct v_inode*
232 vfs_i_alloc();
233
234 void
235 vfs_i_free(struct v_inode* inode);
236
237 int
238 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
239
240 void
241 pcache_init(struct pcache* pcache);
242
243 void
244 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
245
246 struct pcache_pg*
247 pcache_new_page(struct pcache* pcache, uint32_t index);
248
249 void
250 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
251
252 struct pcache_pg*
253 pcache_get_page(struct pcache* pcache,
254                 uint32_t index,
255                 uint32_t* offset,
256                 struct pcache_pg** page);
257
258 int
259 pcache_write(struct v_file* file, void* data, uint32_t len, uint32_t fpos);
260
261 int
262 pcache_read(struct v_file* file, void* data, uint32_t len, uint32_t fpos);
263
264 void
265 pcache_release(struct pcache* pcache);
266
267 int
268 pcache_commit(struct v_file* file, struct pcache_pg* page);
269
270 void
271 pcache_invalidate(struct v_file* file, struct pcache_pg* page);
272
273 void
274 pcache_commit_all(struct v_file* file);
275 #endif /* __LUNAIX_VFS_H */