1 #include <klibc/string.h>
3 #include <lunaix/mm/cake.h>
4 #include <lunaix/mm/page.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/spike.h>
9 #define DNODE_HASHTABLE_BITS 10
10 #define DNODE_HASHTABLE_SIZE (1 << DNODE_HASHTABLE_BITS)
11 #define DNODE_HASH_MASK (DNODE_HASHTABLE_SIZE - 1)
12 #define DNODE_HASHBITS (32 - DNODE_HASHTABLE_BITS)
14 static struct cake_pile* dnode_pile;
15 static struct cake_pile* inode_pile;
16 static struct cake_pile* file_pile;
17 static struct cake_pile* superblock_pile;
19 static struct v_superblock* root_sb;
20 static struct hbucket* dnode_cache;
28 vfs_d_free(struct v_dnode* dnode);
34 vfs_sb_free(struct v_superblock* sb);
39 // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
40 dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
41 inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
42 inode_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
44 cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
46 dnode_cache = valloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
48 // 挂载Lunaix的根文件系统,TwiFS!
49 struct filesystem* rootfs = fsm_get("twifs");
50 root_sb = vfs_sb_alloc();
51 rootfs->mount(root_sb, NULL);
54 inline struct hbucket*
55 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
57 // 与parent的指针值做加法,来减小碰撞的可能性。
58 hash += (uint32_t)parent;
60 hash = hash ^ (hash >> DNODE_HASHBITS);
61 return &dnode_cache[hash & DNODE_HASH_MASK];
65 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
67 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
69 struct v_dnode *pos, *n;
70 hashtable_bucket_foreach(slot, pos, n, hash_list)
72 if (pos->name.hash == str->hash) {
80 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
82 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
83 hlist_add(&bucket->head, &dnode->hash_list);
87 vfs_walk(struct v_dnode* start,
89 struct v_dnode** dentry,
95 if (path[0] == PATH_DELIM) {
96 if ((walk_options & VFS_WALK_FSRELATIVE)) {
97 start = start->super_block->root;
99 start = root_sb->root;
104 struct v_dnode* dnode;
105 struct v_dnode* current_level = start;
107 char name_content[VFS_NAME_MAXLEN];
108 struct hstr name = HSTR(name_content, 0);
110 char current = path[i++], lookahead;
112 lookahead = path[i++];
113 if (current != PATH_DELIM) {
114 if (j >= VFS_NAME_MAXLEN - 1) {
117 if (!VFS_VALID_CHAR(current)) {
120 name_content[j++] = current;
126 // handling cases like /^.*(\/+).*$/
127 if (lookahead == PATH_DELIM) {
132 hstr_rehash(&name, HSTR_FULL_HASH);
134 dnode = vfs_dcache_lookup(current_level, &name);
137 dnode = vfs_d_alloc();
138 dnode->name = HSTR(valloc(VFS_NAME_MAXLEN), j);
139 dnode->name.hash = name.hash;
141 strcpy(dnode->name.value, name_content);
144 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
146 if (errno == VFS_ENOTFOUND &&
147 ((walk_options & VFS_WALK_MKPARENT) ||
148 ((walk_options & VFS_WALK_MKDIR) && !lookahead))) {
151 1) VFS_WALK_MKPARENT:沿路创建所有不存在的文件夹
152 2) VFS_WALK_MKDIR:仅创建路径最后一级的不存在文件夹
154 if (!current_level->inode->ops.mkdir) {
157 errno = current_level->inode->ops.mkdir(
158 current_level->inode, dnode);
166 vfs_dcache_add(current_level, dnode);
168 dnode->parent = current_level;
169 llist_append(¤t_level->children, &dnode->siblings);
173 current_level = dnode;
178 *dentry = current_level;
182 vfree(dnode->name.value);
188 vfs_mount(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
190 struct filesystem* fs = fsm_get(fs_name);
193 struct v_superblock* sb = vfs_sb_alloc();
198 if (!(errno = fs->mount(sb, mnt_point))) {
200 sb->root = mnt_point;
201 mnt_point->super_block = sb;
202 llist_append(&root_sb->sb_list, &sb->sb_list);
209 vfs_mkdir(const char* parent_path,
210 const char* component,
211 struct v_dnode** dentry)
213 return vfs_walk(root_sb->root, parent_path, dentry, VFS_WALK_MKDIR);
217 vfs_unmount(struct v_dnode* mnt_point)
220 struct v_superblock* sb = mnt_point->super_block;
224 if (!(errno = sb->fs->unmount(sb))) {
225 struct v_dnode* fs_root = sb->root;
226 llist_delete(&fs_root->siblings);
227 llist_delete(&sb->sb_list);
234 vfs_open(struct v_dnode* dnode, struct v_file** file)
236 if (!dnode->inode || !dnode->inode->ops.open) {
240 struct v_file* vfile = cake_grab(file_pile);
241 memset(vfile, 0, sizeof(*vfile));
243 int errno = dnode->inode->ops.open(dnode->inode, vfile);
245 cake_release(file_pile, vfile);
253 vfs_close(struct v_file* file)
255 if (!file->ops.close) {
259 int errno = file->ops.close(file);
261 cake_release(file_pile, file);
267 vfs_fsync(struct v_file* file)
269 int errno = VFS_ENOOPS;
270 if (file->ops.sync) {
271 errno = file->ops.sync(file);
273 if (!errno && file->inode->ops.sync) {
274 return file->inode->ops.sync(file->inode);
282 struct v_superblock* sb = cake_grab(superblock_pile);
283 memset(sb, 0, sizeof(*sb));
284 llist_init_head(&sb->sb_list);
289 vfs_sb_free(struct v_superblock* sb)
291 cake_release(superblock_pile, sb);
297 struct v_dnode* dnode = cake_grab(dnode_pile);
298 llist_init_head(&dnode->children);
302 vfs_d_free(struct v_dnode* dnode)
304 if (dnode->ops.destruct) {
305 dnode->ops.destruct(dnode);
307 cake_release(dnode_pile, dnode);
313 struct v_inode* inode = cake_grab(inode_pile);
314 memset(inode, 0, sizeof(*inode));
320 vfs_i_free(struct v_inode* inode)
322 cake_release(inode_pile, inode);