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>
15 #define VFS_NAME_MAXLEN 128
19 #define VFS_IFFILE 0x2
20 #define VFS_IFSEQDEV 0x4
21 #define VFS_IFVOLDEV 0x8
22 #define VFS_IFSYMLINK 0x16
24 #define VFS_WALK_MKPARENT 0x1
25 #define VFS_WALK_FSRELATIVE 0x2
26 #define VFS_WALK_PARENT 0x4
27 #define VFS_WALK_NOFOLLOW 0x8
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)
34 #define FSTYPE_ROFS 0x1
36 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
37 #define DO_STATUS_OR_RETURN(errno) ({ errno < 0 ? DO_STATUS(errno) : errno; })
39 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
41 #define VFS_VALID_CHAR(chr) \
42 (('A' <= (chr) && (chr) <= 'Z') || ('a' <= (chr) && (chr) <= 'z') || \
43 ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' || \
46 typedef uint32_t inode_t;
57 extern struct v_file_ops default_file_ops;
58 extern struct v_inode_ops default_inode_ops;
60 extern struct hstr vfs_ddot;
61 extern struct hstr vfs_dot;
62 extern struct v_dnode* vfs_sysroot;
66 struct hlist_node fs_list;
70 int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
71 int (*unmount)(struct v_superblock* vsb);
76 struct llist_header sb_list;
79 struct filesystem* fs;
81 struct hbucket* i_cache;
84 uint32_t (*read_capacity)(struct v_superblock* vsb);
85 uint32_t (*read_usage)(struct v_superblock* vsb);
86 void (*init_inode)(struct v_superblock* vsb, struct v_inode* inode);
94 void (*read_complete_callback)(struct dir_context* dctx,
102 int (*write)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
103 int (*read)(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
104 int (*readdir)(struct v_file* file, struct dir_context* dctx);
105 int (*seek)(struct v_inode* inode, size_t offset);
106 int (*close)(struct v_file* file);
107 int (*sync)(struct v_file* file);
112 int (*create)(struct v_inode* this, struct v_dnode* dnode);
113 int (*open)(struct v_inode* this, struct v_file* file);
114 int (*sync)(struct v_inode* this);
115 int (*mkdir)(struct v_inode* this, struct v_dnode* dnode);
116 int (*rmdir)(struct v_inode* this, struct v_dnode* dir);
117 int (*unlink)(struct v_inode* this);
118 int (*link)(struct v_inode* this, struct v_dnode* new_name);
119 int (*read_symlink)(struct v_inode* this, const char** path_out);
120 int (*set_symlink)(struct v_inode* this, const char* target);
121 int (*dir_lookup)(struct v_inode* this, struct v_dnode* dnode);
122 int (*rename)(struct v_inode* from_inode,
123 struct v_dnode* from_dnode,
124 struct v_dnode* to_dnode);
129 struct v_inode* inode;
130 struct v_dnode* dnode;
131 struct llist_header* f_list;
133 atomic_ulong ref_count;
134 struct v_file_ops* ops; // for caching
156 struct v_superblock* sb;
157 struct hlist_node hash_list;
159 struct pcache* pg_cache;
160 void* data; // 允许底层FS绑定他的一些专有数据
161 struct v_inode_ops* ops;
162 struct v_file_ops* default_fops;
168 struct llist_header list;
169 struct llist_header submnts;
170 struct llist_header sibmnts;
171 struct v_mount* parent;
172 struct v_dnode* mnt_point;
173 struct v_superblock* super_block;
174 uint32_t busy_counter;
179 mutex_t lock; // sync the path walking
182 struct v_inode* inode;
183 struct v_dnode* parent;
184 struct hlist_node hash_list;
185 struct llist_header children;
186 struct llist_header siblings;
187 struct v_superblock* super_block;
189 atomic_ulong ref_count;
196 struct v_fd* fds[VFS_MAX_FD];
201 struct v_inode* master;
203 struct llist_header pages;
204 struct llist_header dirty;
211 struct llist_header pg_list;
212 struct llist_header dirty_list;
214 struct pcache* holder;
219 /* --- file system manager --- */
227 fsm_new_fs(char* name, size_t name_len);
230 fsm_register(struct filesystem* fs);
233 fsm_get(const char* fs_name);
239 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
242 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
245 vfs_walk(struct v_dnode* start,
247 struct v_dnode** dentry,
248 struct hstr* component,
252 vfs_mount(const char* target, const char* fs_name, struct device* device);
255 vfs_unmount(const char* target);
258 vfs_mount_at(const char* fs_name,
259 struct device* device,
260 struct v_dnode* mnt_point);
263 vfs_unmount_at(struct v_dnode* mnt_point);
266 vfs_mkdir(const char* path, struct v_dnode** dentry);
269 vfs_open(struct v_dnode* dnode, struct v_file** file);
272 vfs_close(struct v_file* file);
275 vfs_fsync(struct v_file* file);
278 vfs_assign_inode(struct v_dnode* assign_to, struct v_inode* inode);
284 vfs_sb_free(struct v_superblock* sb);
290 vfs_d_free(struct v_dnode* dnode);
293 vfs_i_find(struct v_superblock* sb, uint32_t i_id);
296 vfs_i_addhash(struct v_inode* inode);
299 vfs_i_alloc(struct v_superblock* sb);
302 vfs_i_free(struct v_inode* inode);
305 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
308 pcache_init(struct pcache* pcache);
311 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
314 pcache_new_page(struct pcache* pcache, uint32_t index);
317 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
320 pcache_get_page(struct pcache* pcache,
323 struct pcache_pg** page);
326 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
329 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
332 pcache_release(struct pcache* pcache);
335 pcache_commit(struct v_inode* inode, struct pcache_pg* page);
338 pcache_commit_all(struct v_inode* inode);
341 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page);
349 mnt_mkbusy(struct v_mount* mnt);
357 mnt_chillax(struct v_mount* mnt);
360 vfs_mount_root(const char* fs_name, struct device* device);
363 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point);
366 default_file_read(struct v_inode* inode, void* buffer, size_t len, size_t fpos);
369 default_file_write(struct v_inode* inode,
375 default_file_readdir(struct v_file* file, struct dir_context* dctx);
378 default_inode_dirlookup(struct v_inode* this, struct v_dnode* dnode);
381 default_inode_rename(struct v_inode* from_inode,
382 struct v_dnode* from_dnode,
383 struct v_dnode* to_dnode);
386 default_file_close(struct v_file* file);
389 default_file_seek(struct v_inode* inode, size_t offset);
392 default_inode_open(struct v_inode* this, struct v_file* file);
395 default_inode_rmdir(struct v_inode* this, struct v_dnode* dir);
398 default_inode_mkdir(struct v_inode* this, struct v_dnode* dir);
400 #endif /* __LUNAIX_VFS_H */