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/ds/mutex.h>
13 #include <lunaix/status.h>
14 #include <stdatomic.h>
16 #define VFS_NAME_MAXLEN 128
20 #define VFS_IFFILE 0x2
21 #define VFS_IFSEQDEV 0x4
22 #define VFS_IFVOLDEV 0x8
23 #define VFS_IFSYMLINK 0x16
25 #define VFS_WALK_MKPARENT 0x1
26 #define VFS_WALK_FSRELATIVE 0x2
27 #define VFS_WALK_PARENT 0x4
28 #define VFS_WALK_NOFOLLOW 0x4
30 #define FSTYPE_ROFS 0x1
32 #define VFS_VALID_CHAR(chr) \
33 (('A' <= (chr) && (chr) <= 'Z') || ('a' <= (chr) && (chr) <= 'z') || \
34 ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' || \
37 extern struct hstr vfs_ddot;
38 extern struct hstr vfs_dot;
49 struct hlist_node fs_list;
52 int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
53 int (*unmount)(struct v_superblock* vsb);
58 struct llist_header sb_list;
62 struct filesystem* fs;
66 uint32_t (*read_capacity)(struct v_superblock* vsb);
67 uint32_t (*read_usage)(struct v_superblock* vsb);
75 void (*read_complete_callback)(struct dir_context* dctx,
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 (*rename)(struct v_inode* inode, char* new_name);
88 int (*close)(struct v_file* file);
89 int (*sync)(struct v_inode* inode);
94 struct v_inode* inode;
95 struct v_dnode* dnode;
96 struct llist_header* f_list;
98 atomic_ulong ref_count;
99 struct v_file_ops ops;
120 struct pcache* pg_cache;
121 void* data; // 允许底层FS绑定他的一些专有数据
124 int (*create)(struct v_inode* this, struct v_dnode* dnode);
125 int (*open)(struct v_inode* this, struct v_file* file);
126 int (*sync)(struct v_inode* this);
127 int (*mkdir)(struct v_inode* this, struct v_dnode* dnode);
128 int (*rmdir)(struct v_inode* this);
129 int (*unlink)(struct v_inode* this);
130 int (*link)(struct v_inode* this, struct v_dnode* new_name);
131 int (*read_symlink)(struct v_inode* this, const char** path_out);
132 int (*symlink)(struct v_inode* this, const char* target);
133 int (*dir_lookup)(struct v_inode* this, struct v_dnode* dnode);
135 struct v_file_ops default_fops;
140 mutex_t lock; // sync the path walking
142 struct v_inode* inode;
143 struct v_dnode* parent;
144 struct hlist_node hash_list;
145 struct llist_header children;
146 struct llist_header siblings;
147 struct v_superblock* super_block;
148 atomic_ulong ref_count;
151 void (*destruct)(struct v_dnode* dnode);
157 struct v_fd* fds[VFS_MAX_FD];
162 struct llist_header pg_list;
163 struct llist_header dirty_list;
172 struct v_inode* master;
174 struct llist_header pages;
175 struct llist_header dirty;
180 /* --- file system manager --- */
185 fsm_register(struct filesystem* fs);
188 fsm_get(const char* fs_name);
194 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
197 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
200 vfs_walk(struct v_dnode* start,
202 struct v_dnode** dentry,
203 struct hstr* component,
207 vfs_mount(const char* target, const char* fs_name, bdev_t device);
210 vfs_unmount(const char* target);
213 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point);
216 vfs_unmount_at(struct v_dnode* mnt_point);
219 vfs_mkdir(const char* path, struct v_dnode** dentry);
222 vfs_open(struct v_dnode* dnode, struct v_file** file);
225 vfs_close(struct v_file* file);
228 vfs_fsync(struct v_file* file);
234 vfs_sb_free(struct v_superblock* sb);
240 vfs_d_free(struct v_dnode* dnode);
246 vfs_i_free(struct v_inode* inode);
249 vfs_dup_fd(struct v_fd* old, struct v_fd** new);
252 pcache_init(struct pcache* pcache);
255 pcache_release_page(struct pcache* pcache, struct pcache_pg* page);
258 pcache_new_page(struct pcache* pcache, uint32_t index);
261 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg);
264 pcache_get_page(struct pcache* pcache,
267 struct pcache_pg** page);
270 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
273 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos);
276 pcache_release(struct pcache* pcache);
279 pcache_commit(struct v_inode* inode, struct pcache_pg* page);
282 pcache_commit_all(struct v_inode* inode);
285 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page);
286 #endif /* __LUNAIX_VFS_H */