feat: twifs - pseudo file system for lunaix kernel state exposure
[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
10 #define VFS_NAME_MAXLEN 128
11 #define VFS_MAX_FD 32
12
13 #define VFS_INODE_TYPE_DIR 0x1
14 #define VFS_INODE_TYPE_FILE 0x2
15 #define VFS_INODE_TYPE_DEVICE 0x4
16
17 #define VFS_ETOOLONG -1
18 #define VFS_ENOFS -2
19 #define VFS_EBADMNT -3
20 #define VFS_ENODIR -4
21 #define VFS_EENDOFDIR -5
22 #define VFS_ENOTFOUND -6
23 #define VFS_ENOOPS -7
24 #define VFS_EINVLD -8
25 #define VFS_EEOF -9
26
27 #define VFS_WALK_MKPARENT 0x1
28 #define VFS_WALK_FSRELATIVE 0x2
29 #define VFS_WALK_MKDIR 0x4
30
31 #define VFS_IOBUF_FDIRTY 0x1
32
33 #define VFS_VALID_CHAR(chr)                                                    \
34     ('A' <= (chr) && (chr) <= 'Z' || 'a' <= (chr) && (chr) <= 'z' ||           \
35      '0' <= (chr) && (chr) <= '9' || (chr) == '.' || (chr) == '_' ||           \
36      (chr) == '-')
37
38 struct v_dnode;
39
40 struct filesystem
41 {
42     struct hlist_node fs_list;
43     struct hstr fs_name;
44     int (*mount)(struct v_superblock* vsb, struct v_dnode* mount_point);
45     int (*unmount)(struct v_superblock* vsb);
46 };
47
48 struct v_superblock
49 {
50     struct llist_header sb_list;
51     int fs_id;
52     bdev_t dev;
53     struct v_dnode* root;
54     struct filesystem* fs;
55     uint32_t iobuf_size;
56     struct
57     {
58         uint32_t (*read_capacity)(struct v_superblock* vsb);
59         uint32_t (*read_usage)(struct v_superblock* vsb);
60     } ops;
61 };
62
63 struct dir_context
64 {
65     int index;
66     void* cb_data;
67     void (*read_complete_callback)(struct dir_context* dctx,
68                                    const char* name,
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     void* data; // 允许底层FS绑定他的一些专有数据
107     struct
108     {
109         int (*open)(struct v_inode* inode, struct v_file* file);
110         int (*sync)(struct v_inode* inode);
111         int (*mkdir)(struct v_inode* inode, struct v_dnode* dnode);
112         int (*dir_lookup)(struct v_inode* inode, struct v_dnode* dnode);
113     } ops;
114 };
115
116 struct v_dnode
117 {
118     struct hstr name;
119     struct v_inode* inode;
120     struct v_dnode* parent;
121     struct hlist_node hash_list;
122     struct llist_header children;
123     struct llist_header siblings;
124     struct v_superblock* super_block;
125     struct
126     {
127         void (*destruct)(struct v_dnode* dnode);
128     } ops;
129 };
130
131 struct v_fdtable
132 {
133     struct v_fd* fds[VFS_MAX_FD];
134 };
135
136 /* --- file system manager --- */
137 void
138 fsm_init();
139
140 void
141 fsm_register(struct filesystem* fs);
142
143 struct filesystem*
144 fsm_get(const char* fs_name);
145
146 struct v_dnode*
147 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str);
148
149 void
150 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode);
151
152 int
153 vfs_walk(struct v_dnode* start,
154          const char* path,
155          struct v_dnode** dentry,
156          int walk_options);
157
158 int
159 vfs_mount(const char* fs_name, bdev_t device, struct v_dnode* mnt_point);
160
161 int
162 vfs_unmount(struct v_dnode* mnt_point);
163
164 int
165 vfs_mkdir(const char* parent_path,
166           const char* component,
167           struct v_dnode** dentry);
168
169 int
170 vfs_open(struct v_dnode* dnode, struct v_file** file);
171
172 int
173 vfs_close(struct v_file* file);
174
175 int
176 vfs_fsync(struct v_file* file);
177
178 struct v_superblock*
179 vfs_sb_alloc();
180
181 void
182 vfs_sb_free(struct v_superblock* sb);
183
184 struct v_dnode*
185 vfs_d_alloc();
186
187 void
188 vfs_d_free(struct v_dnode* dnode);
189
190 struct v_inode*
191 vfs_i_alloc();
192
193 void
194 vfs_i_free(struct v_inode* inode);
195 #endif /* __LUNAIX_VFS_H */