1 #include <klibc/string.h>
3 #include <lunaix/mm/cake.h>
4 #include <lunaix/mm/valloc.h>
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)
12 #define VFS_ETOOLONG -1
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;
20 static struct v_superblock* root_sb;
21 static struct hbucket* dnode_cache;
29 __free_dnode(struct v_dnode* dnode);
35 __free_superblock(struct v_superblock* sb);
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);
45 cake_new_pile("superblock", sizeof(struct v_superblock), 1, 0);
47 dnode_cache = valloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
49 // 挂载Lunaix的根文件系统,TwiFS!
50 struct filesystem* rootfs = fsm_get("twifs");
51 root_sb = __new_superblock();
52 rootfs->mount(root_sb, NULL);
55 inline struct hbucket*
56 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
58 // 与parent的指针值做加法,来减小碰撞的可能性。
59 hash += (uint32_t)parent;
61 hash = hash ^ (hash >> DNODE_HASHBITS);
62 return &dnode_cache[hash & DNODE_HASH_MASK];
66 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
68 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
70 struct v_dnode *pos, *n;
71 hashtable_bucket_foreach(slot, pos, n, hash_list)
73 if (pos->name.hash == str->hash) {
81 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
83 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
84 hlist_add(&bucket->head, &dnode->hash_list);
88 vfs_walk(struct v_dnode* start, const char* path, struct v_dnode** dentry)
93 if (path[0] == PATH_DELIM) {
94 start = start->super_block->root;
98 struct v_dnode* dnode;
99 struct v_dnode* current_level = start;
101 char name_content[VFS_NAME_MAXLEN];
102 struct hstr name = HSTR(name_content, 0);
104 char current, lookahead;
107 lookahead = path[i++];
108 if (current != PATH_DELIM) {
109 if (j >= VFS_NAME_MAXLEN - 1) {
110 errno = VFS_ETOOLONG;
113 name_content[j++] = current;
120 hstr_rehash(&name, 32);
122 dnode = vfs_dcache_lookup(current_level, &name);
125 dnode = __new_dnode();
126 strcpy(dnode->name.value, name_content);
127 dnode->name.hash = name.hash;
131 dnode->inode->ops.dir_lookup(current_level->inode, dnode))) {
135 vfs_dcache_add(current_level, dnode);
137 dnode->parent = current_level;
138 llist_append(¤t_level->children, &dnode->siblings);
142 current_level = dnode;
154 vfs_mount(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
156 struct filesystem* fs = fsm_get(fs_name);
159 struct v_superblock* sb = __new_superblock();
164 if (!(errno = fs->mount(sb, mnt_point))) {
166 llist_append(&root_sb->sb_list, &sb->sb_list);
173 vfs_unmount(const int fs_id)
176 struct v_superblock *pos, *n;
177 llist_for_each(pos, n, &root_sb->sb_list, sb_list)
179 if (pos->fs_id != fs_id) {
182 if (!(errno = pos->fs->unmount(pos))) {
183 llist_delete(&pos->sb_list);
184 __free_superblock(pos);
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();
201 __free_superblock(struct v_superblock* sb)
203 __free_dnode(sb->root);
204 cake_release(superblock_pile, sb);
210 struct v_dnode* dnode = cake_grab(dnode_pile);
211 dnode->inode = cake_grab(inode_pile);
212 dnode->name.value = cake_grab(name_pile);
216 __free_dnode(struct v_dnode* dnode)
218 cake_release(inode_pile, dnode->inode);
219 cake_release(name_pile, dnode->name.value);
220 cake_release(dnode_pile, dnode);