3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief Lunaix virtual file system - an abstraction layer for all file system.
8 * @copyright Copyright (c) 2022
12 #include <klibc/string.h>
13 #include <lunaix/dirent.h>
14 #include <lunaix/foptions.h>
15 #include <lunaix/fs.h>
16 #include <lunaix/mm/cake.h>
17 #include <lunaix/mm/page.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/process.h>
20 #include <lunaix/spike.h>
21 #include <lunaix/syscall.h>
23 #define PATH_DELIM '/'
24 #define DNODE_HASHTABLE_BITS 10
25 #define DNODE_HASHTABLE_SIZE (1 << DNODE_HASHTABLE_BITS)
26 #define DNODE_HASH_MASK (DNODE_HASHTABLE_SIZE - 1)
27 #define DNODE_HASHBITS (32 - DNODE_HASHTABLE_BITS)
29 static struct cake_pile* dnode_pile;
30 static struct cake_pile* inode_pile;
31 static struct cake_pile* file_pile;
32 static struct cake_pile* superblock_pile;
33 static struct cake_pile* fd_pile;
35 static struct v_superblock* root_sb;
36 static struct hbucket* dnode_cache;
44 vfs_d_free(struct v_dnode* dnode);
50 vfs_sb_free(struct v_superblock* sb);
55 // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
56 dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
57 inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
58 file_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
59 fd_pile = cake_new_pile("fd_cache", sizeof(struct v_fd), 1, 0);
61 cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
63 dnode_cache = vzalloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
65 // 创建一个根superblock,用来蕴含我们的根目录。
66 root_sb = vfs_sb_alloc();
67 root_sb->root = vfs_d_alloc();
70 inline struct hbucket*
71 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
73 // 与parent的指针值做加法,来减小碰撞的可能性。
74 hash += (uint32_t)parent;
76 hash = hash ^ (hash >> DNODE_HASHBITS);
77 return &dnode_cache[hash & DNODE_HASH_MASK];
81 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
83 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
85 struct v_dnode *pos, *n;
86 hashtable_bucket_foreach(slot, pos, n, hash_list)
88 if (pos->name.hash == str->hash) {
96 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
98 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
99 hlist_add(&bucket->head, &dnode->hash_list);
103 vfs_walk(struct v_dnode* start,
105 struct v_dnode** dentry,
106 struct hstr* component,
112 if (path[0] == PATH_DELIM) {
113 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
114 start = start->super_block->root;
116 start = root_sb->root;
121 struct v_dnode* dnode;
122 struct v_dnode* current_level = start;
124 char name_content[VFS_NAME_MAXLEN];
125 struct hstr name = HSTR(name_content, 0);
127 char current = path[i++], lookahead;
129 lookahead = path[i++];
130 if (current != PATH_DELIM) {
131 if (j >= VFS_NAME_MAXLEN - 1) {
134 if (!VFS_VALID_CHAR(current)) {
137 name_content[j++] = current;
143 // handling cases like /^.*(\/+).*$/
144 if (lookahead == PATH_DELIM) {
149 hstr_rehash(&name, HSTR_FULL_HASH);
151 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
153 component->hash = name.hash;
155 strcpy(component->value, name_content);
160 dnode = vfs_dcache_lookup(current_level, &name);
163 dnode = vfs_d_alloc();
164 dnode->name = HSTR(valloc(VFS_NAME_MAXLEN), j);
165 dnode->name.hash = name.hash;
167 strcpy(dnode->name.value, name_content);
170 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
172 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
173 if (!current_level->inode->ops.mkdir) {
176 errno = current_level->inode->ops.mkdir(
177 current_level->inode, dnode);
185 vfs_dcache_add(current_level, dnode);
187 dnode->parent = current_level;
188 llist_append(¤t_level->children, &dnode->siblings);
192 current_level = dnode;
197 *dentry = current_level;
201 vfree(dnode->name.value);
207 vfs_mount(const char* target, const char* fs_name, bdev_t device)
212 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
213 errno = vfs_mount_at(fs_name, device, mnt);
220 vfs_unmount(const char* target)
225 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
226 errno = vfs_unmount_at(mnt);
233 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
235 struct filesystem* fs = fsm_get(fs_name);
238 struct v_superblock* sb = vfs_sb_alloc();
243 if (!(errno = fs->mount(sb, mnt_point))) {
245 sb->root = mnt_point;
246 mnt_point->super_block = sb;
247 llist_append(&root_sb->sb_list, &sb->sb_list);
254 vfs_unmount_at(struct v_dnode* mnt_point)
257 struct v_superblock* sb = mnt_point->super_block;
261 if (!(errno = sb->fs->unmount(sb))) {
262 struct v_dnode* fs_root = sb->root;
263 llist_delete(&fs_root->siblings);
264 llist_delete(&sb->sb_list);
271 vfs_open(struct v_dnode* dnode, struct v_file** file)
273 if (!dnode->inode || !dnode->inode->ops.open) {
277 struct v_file* vfile = cake_grab(file_pile);
278 memset(vfile, 0, sizeof(*vfile));
280 int errno = dnode->inode->ops.open(dnode->inode, vfile);
282 cake_release(file_pile, vfile);
290 vfs_close(struct v_file* file)
292 if (!file->ops.close) {
296 int errno = file->ops.close(file);
298 cake_release(file_pile, file);
304 vfs_fsync(struct v_file* file)
307 if (file->ops.sync) {
308 errno = file->ops.sync(file);
310 if (!errno && file->inode->ops.sync) {
311 return file->inode->ops.sync(file->inode);
317 vfs_alloc_fdslot(int* fd)
319 for (size_t i = 0; i < VFS_MAX_FD; i++) {
320 if (!__current->fdtable->fds[i]) {
331 struct v_superblock* sb = cake_grab(superblock_pile);
332 memset(sb, 0, sizeof(*sb));
333 llist_init_head(&sb->sb_list);
338 vfs_sb_free(struct v_superblock* sb)
340 cake_release(superblock_pile, sb);
346 struct v_dnode* dnode = cake_grab(dnode_pile);
347 llist_init_head(&dnode->children);
351 vfs_d_free(struct v_dnode* dnode)
353 if (dnode->ops.destruct) {
354 dnode->ops.destruct(dnode);
356 cake_release(dnode_pile, dnode);
362 struct v_inode* inode = cake_grab(inode_pile);
363 memset(inode, 0, sizeof(*inode));
369 vfs_i_free(struct v_inode* inode)
371 cake_release(inode_pile, inode);
374 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
376 char name_str[VFS_NAME_MAXLEN];
377 struct hstr name = HSTR(name_str, 0);
378 struct v_dnode *dentry, *file;
380 if ((errno = vfs_walk(NULL, path, &dentry, &name, VFS_WALK_PARENT))) {
384 struct v_file* opened_file = 0;
385 if (!(file = vfs_dcache_lookup(dentry, &name)) && (options & FO_CREATE)) {
386 errno = dentry->inode->ops.create(dentry->inode, opened_file);
390 errno = vfs_open(file, &opened_file);
393 __current->k_status = errno;
395 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
396 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
397 fd_s->file = opened_file;
398 fd_s->pos = file->inode->fsize & -((options & FO_APPEND) == 0);
399 __current->fdtable->fds[fd] = fd_s;
402 return SYSCALL_ESTATUS(errno);
405 __DEFINE_LXSYSCALL1(int, close, int, fd)
409 if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
411 } else if (!(errno = vfs_close(fd_s->file))) {
415 __current->k_status = errno;
417 return SYSCALL_ESTATUS(errno);
421 __vfs_readdir_callback(struct dir_context* dctx,
426 struct dirent* dent = (struct dirent*)dctx->cb_data;
427 strcpy(dent->d_name, name);
429 dent->d_type = dtype;
432 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
436 if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
438 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
441 struct dir_context dctx =
442 (struct dir_context){ .cb_data = dent,
443 .index = dent->d_offset,
444 .read_complete_callback =
445 __vfs_readdir_callback };
446 if (!(errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
451 __current->k_status = errno;
452 return SYSCALL_ESTATUS(errno);
455 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
457 struct v_dnode *parent, *dir;
458 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
460 vfs_walk(root_sb->root, path, &parent, &component, VFS_WALK_PARENT);
465 if (!parent->inode->ops.mkdir) {
467 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
471 dir->name = component;
472 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
473 llist_append(&parent->children, &dir->siblings);
476 vfree(component.value);
481 __current->k_status = errno;
482 return SYSCALL_ESTATUS(errno);