feat: vfs - path walking and dnode caching.
[lunaix-os.git] / lunaix-os / kernel / fs / vfs.c
1 #include <klibc/string.h>
2 #include <lunaix/fs.h>
3 #include <lunaix/mm/cake.h>
4 #include <lunaix/mm/valloc.h>
5
6 #define PATH_DELIM '/'
7 #define DNODE_HASHTABLE_BITS 10
8 #define DNODE_HASHTABLE_SIZE (1 << DNODE_HASHTABLE_BITS)
9 #define DNODE_HASH_MASK (DNODE_HASHTABLE_SIZE - 1)
10 #define DNODE_HASHBITS (32 - DNODE_HASHTABLE_BITS)
11
12 #define VFS_ETOOLONG -1
13 #define VFS_ENOFS -2
14
15 static struct cake_pile* dnode_pile;
16 static struct cake_pile* inode_pile;
17 static struct cake_pile* superblock_pile;
18 static struct cake_pile* name_pile;
19
20 static struct v_superblock* root_sb;
21 static struct hbucket* dnode_cache;
22
23 static int fs_id = 0;
24
25 struct v_dnode*
26 __new_dnode();
27
28 void
29 __free_dnode(struct v_dnode* dnode);
30
31 struct v_superblock*
32 __new_superblock();
33
34 void
35 __free_superblock(struct v_superblock* sb);
36
37 void
38 vfs_init()
39 {
40     // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内部碎片的产生
41     dnode_pile = cake_new_pile("dnode", sizeof(struct v_dnode), 1, 0);
42     inode_pile = cake_new_pile("inode", sizeof(struct v_inode), 1, 0);
43     name_pile = cake_new_pile("dname", VFS_NAME_MAXLEN, 1, 0);
44     superblock_pile =
45       cake_new_pile("superblock", sizeof(struct v_superblock), 1, 0);
46
47     dnode_cache = valloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
48
49     // 挂载Lunaix的根文件系统,TwiFS!
50     struct filesystem* rootfs = fsm_get("twifs");
51     root_sb = __new_superblock();
52     rootfs->mount(root_sb, NULL);
53 }
54
55 inline struct hbucket*
56 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
57 {
58     // 与parent的指针值做加法,来减小碰撞的可能性。
59     hash += (uint32_t)parent;
60     // 确保低位更加随机
61     hash = hash ^ (hash >> DNODE_HASHBITS);
62     return &dnode_cache[hash & DNODE_HASH_MASK];
63 }
64
65 struct v_dnode*
66 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
67 {
68     struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
69
70     struct v_dnode *pos, *n;
71     hashtable_bucket_foreach(slot, pos, n, hash_list)
72     {
73         if (pos->name.hash == str->hash) {
74             return pos;
75         }
76     }
77     return NULL;
78 }
79
80 void
81 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
82 {
83     struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
84     hlist_add(&bucket->head, &dnode->hash_list);
85 }
86
87 int
88 vfs_walk(struct v_dnode* start, const char* path, struct v_dnode** dentry)
89 {
90     int errno = 0;
91     int i = 0, j = 0;
92
93     if (path[0] == PATH_DELIM) {
94         start = start->super_block->root;
95         i++;
96     }
97
98     struct v_dnode* dnode;
99     struct v_dnode* current_level = start;
100
101     char name_content[VFS_NAME_MAXLEN];
102     struct hstr name = HSTR(name_content, 0);
103
104     char current, lookahead;
105     do {
106         current = path[i++];
107         lookahead = path[i++];
108         if (current != PATH_DELIM) {
109             if (j >= VFS_NAME_MAXLEN - 1) {
110                 errno = VFS_ETOOLONG;
111                 goto error;
112             }
113             name_content[j++] = current;
114             if (lookahead) {
115                 continue;
116             }
117         }
118
119         name_content[j] = 0;
120         hstr_rehash(&name, 32);
121
122         dnode = vfs_dcache_lookup(current_level, &name);
123
124         if (!dnode) {
125             dnode = __new_dnode();
126             strcpy(dnode->name.value, name_content);
127             dnode->name.hash = name.hash;
128             dnode->name.len = j;
129
130             if ((errno =
131                    dnode->inode->ops.dir_lookup(current_level->inode, dnode))) {
132                 goto error;
133             }
134
135             vfs_dcache_add(current_level, dnode);
136
137             dnode->parent = current_level;
138             llist_append(&current_level->children, &dnode->siblings);
139         }
140
141         j = 0;
142         current_level = dnode;
143     } while (lookahead);
144
145     *dentry = dnode;
146     return 0;
147
148 error:
149     __free_dnode(dnode);
150     return errno;
151 }
152
153 int
154 vfs_mount(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
155 {
156     struct filesystem* fs = fsm_get(fs_name);
157     if (!fs)
158         return VFS_ENOFS;
159     struct v_superblock* sb = __new_superblock();
160     sb->dev = device;
161     sb->fs_id = fs_id++;
162
163     int errno = 0;
164     if (!(errno = fs->mount(sb, mnt_point))) {
165         sb->fs = fs;
166         llist_append(&root_sb->sb_list, &sb->sb_list);
167     }
168
169     return errno;
170 }
171
172 int
173 vfs_unmount(const int fs_id)
174 {
175     int errno = 0;
176     struct v_superblock *pos, *n;
177     llist_for_each(pos, n, &root_sb->sb_list, sb_list)
178     {
179         if (pos->fs_id != fs_id) {
180             continue;
181         }
182         if (!(errno = pos->fs->unmount(pos))) {
183             llist_delete(&pos->sb_list);
184             __free_superblock(pos);
185         }
186         break;
187     }
188     return errno;
189 }
190
191 struct v_superblock*
192 __new_superblock()
193 {
194     struct v_superblock* sb = cake_grab(superblock_pile);
195     memset(sb, 0, sizeof(*sb));
196     llist_init_head(&sb->sb_list);
197     sb->root = __new_dnode();
198 }
199
200 void
201 __free_superblock(struct v_superblock* sb)
202 {
203     __free_dnode(sb->root);
204     cake_release(superblock_pile, sb);
205 }
206
207 struct v_dnode*
208 __new_dnode()
209 {
210     struct v_dnode* dnode = cake_grab(dnode_pile);
211     dnode->inode = cake_grab(inode_pile);
212     dnode->name.value = cake_grab(name_pile);
213 }
214
215 void
216 __free_dnode(struct v_dnode* dnode)
217 {
218     cake_release(inode_pile, dnode->inode);
219     cake_release(name_pile, dnode->name.value);
220     cake_release(dnode_pile, dnode);
221 }