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;
40 struct hstr vfs_ddot = HSTR("..", 2);
41 struct hstr vfs_dot = HSTR(".", 1);
47 vfs_d_free(struct v_dnode* dnode);
53 vfs_sb_free(struct v_superblock* sb);
58 // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
59 dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
60 inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
61 file_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
62 fd_pile = cake_new_pile("fd_cache", sizeof(struct v_fd), 1, 0);
64 cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
66 dnode_cache = vzalloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
68 hstr_rehash(&vfs_ddot, HSTR_FULL_HASH);
69 hstr_rehash(&vfs_dot, HSTR_FULL_HASH);
71 // 创建一个根superblock,用来蕴含我们的根目录。
72 root_sb = vfs_sb_alloc();
73 root_sb->root = vfs_d_alloc();
76 inline struct hbucket*
77 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
79 // 与parent的指针值做加法,来减小碰撞的可能性。
80 hash += (uint32_t)parent;
82 hash = hash ^ (hash >> DNODE_HASHBITS);
83 return &dnode_cache[hash & DNODE_HASH_MASK];
87 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
92 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
94 struct v_dnode *pos, *n;
95 hashtable_bucket_foreach(slot, pos, n, hash_list)
97 if (pos->name.hash == str->hash) {
105 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
107 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
108 hlist_add(&bucket->head, &dnode->hash_list);
112 vfs_walk(struct v_dnode* start,
114 struct v_dnode** dentry,
115 struct hstr* component,
121 if (path[0] == PATH_DELIM || !start) {
122 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
123 start = start->super_block->root;
125 start = root_sb->root;
130 struct v_dnode* dnode;
131 struct v_dnode* current_level = start;
133 char name_content[VFS_NAME_MAXLEN];
134 struct hstr name = HSTR(name_content, 0);
136 char current = path[i++], lookahead;
138 lookahead = path[i++];
139 if (current != PATH_DELIM) {
140 if (j >= VFS_NAME_MAXLEN - 1) {
143 if (!VFS_VALID_CHAR(current)) {
146 name_content[j++] = current;
152 // handling cases like /^.*(\/+).*$/
153 if (lookahead == PATH_DELIM) {
159 hstr_rehash(&name, HSTR_FULL_HASH);
161 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
163 component->hash = name.hash;
165 strcpy(component->value, name_content);
170 dnode = vfs_dcache_lookup(current_level, &name);
173 dnode = vfs_d_alloc();
174 dnode->name = HSTR(valloc(VFS_NAME_MAXLEN), j);
175 dnode->name.hash = name.hash;
177 strcpy(dnode->name.value, name_content);
180 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
182 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
183 if (!current_level->inode->ops.mkdir) {
186 errno = current_level->inode->ops.mkdir(
187 current_level->inode, dnode);
195 vfs_dcache_add(current_level, dnode);
197 dnode->parent = current_level;
198 llist_append(¤t_level->children, &dnode->siblings);
202 current_level = dnode;
207 *dentry = current_level;
211 vfree(dnode->name.value);
218 vfs_mount(const char* target, const char* fs_name, bdev_t device)
223 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
224 errno = vfs_mount_at(fs_name, device, mnt);
231 vfs_unmount(const char* target)
236 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
237 errno = vfs_unmount_at(mnt);
244 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
246 struct filesystem* fs = fsm_get(fs_name);
249 struct v_superblock* sb = vfs_sb_alloc();
254 if (!(errno = fs->mount(sb, mnt_point))) {
256 sb->root = mnt_point;
257 mnt_point->super_block = sb;
258 llist_append(&root_sb->sb_list, &sb->sb_list);
265 vfs_unmount_at(struct v_dnode* mnt_point)
268 struct v_superblock* sb = mnt_point->super_block;
272 if (!(errno = sb->fs->unmount(sb))) {
273 struct v_dnode* fs_root = sb->root;
274 llist_delete(&fs_root->siblings);
275 llist_delete(&sb->sb_list);
282 vfs_open(struct v_dnode* dnode, struct v_file** file)
284 if (!dnode->inode || !dnode->inode->ops.open) {
288 struct v_file* vfile = cake_grab(file_pile);
289 memset(vfile, 0, sizeof(*vfile));
291 int errno = dnode->inode->ops.open(dnode->inode, vfile);
293 cake_release(file_pile, vfile);
301 vfs_close(struct v_file* file)
303 if (!file->ops.close) {
307 int errno = file->ops.close(file);
309 cake_release(file_pile, file);
315 vfs_fsync(struct v_file* file)
318 if (file->ops.sync) {
319 errno = file->ops.sync(file);
321 if (!errno && file->inode->ops.sync) {
322 return file->inode->ops.sync(file->inode);
328 vfs_alloc_fdslot(int* fd)
330 for (size_t i = 0; i < VFS_MAX_FD; i++) {
331 if (!__current->fdtable->fds[i]) {
342 struct v_superblock* sb = cake_grab(superblock_pile);
343 memset(sb, 0, sizeof(*sb));
344 llist_init_head(&sb->sb_list);
349 vfs_sb_free(struct v_superblock* sb)
351 cake_release(superblock_pile, sb);
357 struct v_dnode* dnode = cake_grab(dnode_pile);
358 memset(dnode, 0, sizeof(*dnode));
359 llist_init_head(&dnode->children);
364 vfs_d_free(struct v_dnode* dnode)
366 if (dnode->ops.destruct) {
367 dnode->ops.destruct(dnode);
369 cake_release(dnode_pile, dnode);
375 struct v_inode* inode = cake_grab(inode_pile);
376 memset(inode, 0, sizeof(*inode));
382 vfs_i_free(struct v_inode* inode)
384 cake_release(inode_pile, inode);
388 __vfs_do_open(struct v_file** file_out, const char* path, int options)
390 char name_str[VFS_NAME_MAXLEN];
391 struct hstr name = HSTR(name_str, 0);
392 struct v_dnode *dentry, *file;
394 if ((errno = vfs_walk(NULL, path, &dentry, &name, VFS_WALK_PARENT))) {
398 vfs_walk(dentry, name.value, &file, NULL, 0);
400 struct v_file* opened_file = 0;
402 if ((options & FO_CREATE)) {
403 errno = dentry->inode->ops.create(dentry->inode, opened_file);
408 errno = vfs_open(file, &opened_file);
411 *file_out = opened_file;
415 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
417 struct v_file* opened_file;
418 int errno = __vfs_do_open(&opened_file, path, options), fd;
420 __current->k_status = errno;
422 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
423 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
424 fd_s->file = opened_file;
425 fd_s->pos = opened_file->inode->fsize & -((options & FO_APPEND) != 0);
426 __current->fdtable->fds[fd] = fd_s;
430 return SYSCALL_ESTATUS(errno);
433 #define GET_FD(fd, fd_s) \
434 (fd >= 0 && fd < VFS_MAX_FD && (fd_s = __current->fdtable->fds[fd]))
436 __DEFINE_LXSYSCALL1(int, close, int, fd)
440 if (!GET_FD(fd, fd_s)) {
442 } else if (!(errno = vfs_close(fd_s->file))) {
444 __current->fdtable->fds[fd] = 0;
447 __current->k_status = errno;
449 return SYSCALL_ESTATUS(errno);
453 __vfs_readdir_callback(struct dir_context* dctx,
458 struct dirent* dent = (struct dirent*)dctx->cb_data;
459 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
461 dent->d_type = dtype;
464 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
468 if (!GET_FD(fd, fd_s)) {
470 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
473 struct dir_context dctx =
474 (struct dir_context){ .cb_data = dent,
475 .index = dent->d_offset,
476 .read_complete_callback =
477 __vfs_readdir_callback };
478 if (!(errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
483 __current->k_status = errno;
484 return SYSCALL_ESTATUS(errno);
487 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
489 struct v_dnode *parent, *dir;
490 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
491 int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
496 if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
498 } else if (!parent->inode->ops.mkdir) {
500 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
504 dir->name = component;
505 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
506 llist_append(&parent->children, &dir->siblings);
509 vfree(component.value);
514 __current->k_status = errno;
515 return SYSCALL_ESTATUS(errno);
518 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
522 if (!GET_FD(fd, fd_s)) {
525 struct v_file* file = fd_s->file;
526 file->f_pos = fd_s->pos;
527 if ((errno = file->ops.read(file, buf, count)) >= 0) {
532 __current->k_status = errno;
533 return SYSCALL_ESTATUS(errno);
536 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
540 if (!GET_FD(fd, fd_s)) {
543 struct v_file* file = fd_s->file;
544 file->f_pos = fd_s->pos;
545 if ((errno = file->ops.write(file, buf, count)) >= 0) {
550 __current->k_status = errno;
551 return SYSCALL_ESTATUS(errno);
554 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
558 if (!GET_FD(fd, fd_s)) {
561 size_t fpos = fd_s->file->f_pos;
564 fpos = (size_t)((int)fd_s->file->f_pos + offset);
567 fpos = (size_t)((int)fd_s->file->inode->fsize + offset);
579 __current->k_status = errno;
580 return SYSCALL_ESTATUS(errno);