feat: open(2), close(2), mkdir(2) and readdir(2) syscall
[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/ds/hashtable.h>
7 #include <lunaix/ds/hstr.h>
8 #include <lunaix/ds/llist.h>
9 #include <lunaix/status.h>
10
11 #define VFS_NAME_MAXLEN 128
12 #define VFS_MAX_FD 32
13
14 #define VFS_INODE_TYPE_DIR 0x1
15 #define VFS_INODE_TYPE_FILE 0x2
16 #define VFS_INODE_TYPE_DEVICE 0x4
17
18 #define VFS_ENOFS -2
19 #define VFS_EBADMNT -3
20
21 #define VFS_EENDOFDIR -5
22
23 #define VFS_EINVLD -8
24 #define VFS_EEOF -9
25
26 #define VFS_WALK_MKPARENT 0x1
27 #define VFS_WALK_FSRELATIVE 0x2
28 #define VFS_WALK_PARENT 0x4
29
30 #define VFS_IOBUF_FDIRTY 0x1
31
32 #define VFS_VALID_CHAR(chr)                                                    \
33     ('A' <= (chr) && (chr) <= 'Z' || 'a' <= (chr) && (chr) <= 'z' ||           \
34      '0' <= (chr) && (chr) <= '9' || (chr) == '.' || (chr) == '_' ||           \
35      (chr) == '-')
36
37 struct v_dnode;
38
39 struct filesystem
40 {
41     struct hlist_node fs_list;
42     struct hstr fs_name;
43     int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
44     int (*unmount)(struct v_superblock* vsb);
45 };
46
47 struct v_superblock
48 {
49     struct llist_header sb_list;
50     int fs_id;
51     bdev_t dev;
52     struct v_dnode* root;
53     struct filesystem* fs;
54     uint32_t iobuf_size;
55     struct
56     {
57         uint32_t (*read_capacity)(struct v_superblock* vsb);
58         uint32_t (*read_usage)(struct v_superblock* vsb);
59     } ops;
60 };
61
62 struct dir_context
63 {
64     int index;
65     void* cb_data;
66     void (*read_complete_callback)(struct dir_context* dctx,
67                                    const char* name,
68                                    const int len,
69                                    const int dtype);
70 };
71
72 struct v_file_ops
73 {
74     int (*write)(struct v_file* file, void* buffer, size_t len);
75     int (*read)(struct v_file* file, void* buffer, size_t len);
76     int (*readdir)(struct v_file* file, struct dir_context* dctx);
77     int (*seek)(struct v_file* file, size_t offset);
78     int (*rename)(struct v_file* file, char* new_name);
79     int (*close)(struct v_file* file);
80     int (*sync)(struct v_file* file);
81 };
82
83 struct v_file
84 {
85     struct v_inode* inode;
86     struct llist_header* f_list;
87     uint32_t f_pos;
88     void* data; // 允许底层FS绑定他的一些专有数据
89     struct v_file_ops ops;
90 };
91
92 struct v_fd
93 {
94     struct v_file* file;
95     int pos;
96 };
97
98 struct v_inode
99 {
100     uint32_t itype;
101     uint32_t ctime;
102     uint32_t mtime;
103     uint64_t lb_addr;
104     uint32_t ref_count;
105     uint32_t lb_usage;
106     uint32_t fsize;
107     void* data; // 允许底层FS绑定他的一些专有数据
108     struct
109     {
110         int (*create)(struct v_inode* inode, struct v_file* file);
111         int (*open)(struct v_inode* inode, struct v_file* file);
112         int (*sync)(struct v_inode* inode);
113         int (*mkdir)(struct v_inode* inode, struct v_dnode* dnode);
114         int (*dir_lookup)(struct v_inode* inode, struct v_dnode* dnode);
115     } ops;
116 };
117
118 struct v_dnode
119 {
120     struct hstr name;
121     struct v_inode* inode;
122     struct v_dnode* parent;
123     struct hlist_node hash_list;
124     struct llist_header children;
125     struct llist_header siblings;
126     struct v_superblock* super_block;
127     struct
128     {
129         void (*destruct)(struct v_dnode* dnode);
130     } ops;
131 };
132
133 struct v_fdtable
134 {
135     struct v_fd* fds[VFS_MAX_FD];
136 };
137
138 /* --- file system manager --- */
139 void
140 fsm_init();
141
142 void
143 fsm_register(struct filesystem* fs);
144
145 struct filesystem*
146 fsm_get(const char* fs_name);
147
148 void
149 vfs_init();
150
151 struct v_dnode*
152 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
153
154 void
155 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
156
157 int
158 vfs_walk(struct v_dnode* start,
159          const char* path,
160          struct v_dnode** dentry,
161          struct hstr* component,
162          int walk_options);
163
164 int
165 vfs_mount(const char* target, const char* fs_name, bdev_t device);
166
167 int
168 vfs_unmount(const char* target);
169
170 int
171 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point);
172
173 int
174 vfs_unmount_at(struct v_dnode* mnt_point);
175
176 int
177 vfs_mkdir(const char* path, struct v_dnode** dentry);
178
179 int
180 vfs_open(struct v_dnode* dnode, struct v_file** file);
181
182 int
183 vfs_close(struct v_file* file);
184
185 int
186 vfs_fsync(struct v_file* file);
187
188 struct v_superblock*
189 vfs_sb_alloc();
190
191 void
192 vfs_sb_free(struct v_superblock* sb);
193
194 struct v_dnode*
195 vfs_d_alloc();
196
197 void
198 vfs_d_free(struct v_dnode* dnode);
199
200 struct v_inode*
201 vfs_i_alloc();
202
203 void
204 vfs_i_free(struct v_inode* inode);
205 #endif /* __LUNAIX_VFS_H */