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);
42 struct hstr vfs_empty = HSTR("", 0);
48 vfs_d_free(struct v_dnode* dnode);
54 vfs_sb_free(struct v_superblock* sb);
59 // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
60 dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
61 inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
62 file_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
63 fd_pile = cake_new_pile("fd_cache", sizeof(struct v_fd), 1, 0);
65 cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
67 dnode_cache = vzalloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
69 hstr_rehash(&vfs_ddot, HSTR_FULL_HASH);
70 hstr_rehash(&vfs_dot, HSTR_FULL_HASH);
72 // 创建一个根superblock,用来蕴含我们的根目录。
73 root_sb = vfs_sb_alloc();
74 root_sb->root = vfs_d_alloc();
77 inline struct hbucket*
78 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
80 // 与parent的指针值做加法,来减小碰撞的可能性。
81 hash += (uint32_t)parent;
83 hash = hash ^ (hash >> DNODE_HASHBITS);
84 return &dnode_cache[hash & DNODE_HASH_MASK];
88 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
90 if (!str->len || HSTR_EQ(str, &vfs_dot))
93 if (HSTR_EQ(str, &vfs_ddot)) {
94 return parent->parent ? parent->parent : parent;
97 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
99 struct v_dnode *pos, *n;
100 hashtable_bucket_foreach(slot, pos, n, hash_list)
102 if (pos->name.hash == str->hash) {
110 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
112 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
113 hlist_add(&bucket->head, &dnode->hash_list);
117 __vfs_walk(struct v_dnode* start,
119 struct v_dnode** dentry,
120 struct hstr* component,
126 if (path[0] == PATH_DELIM || !start) {
127 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
128 start = start->super_block->root;
130 start = root_sb->root;
135 struct v_dnode* dnode;
136 struct v_dnode* current_level = start;
138 char name_content[VFS_NAME_MAXLEN];
139 struct hstr name = HSTR(name_content, 0);
141 char current = path[i++], lookahead;
143 lookahead = path[i++];
144 if (current != PATH_DELIM) {
145 if (j >= VFS_NAME_MAXLEN - 1) {
148 if (!VFS_VALID_CHAR(current)) {
151 name_content[j++] = current;
157 // handling cases like /^.*(\/+).*$/
158 if (lookahead == PATH_DELIM) {
164 hstr_rehash(&name, HSTR_FULL_HASH);
166 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
168 component->hash = name.hash;
170 strcpy(component->value, name_content);
175 dnode = vfs_dcache_lookup(current_level, &name);
178 dnode = vfs_d_alloc();
179 dnode->name = HHSTR(valloc(VFS_NAME_MAXLEN), j, name.hash);
181 strcpy(dnode->name.value, name_content);
184 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
186 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
187 if (!current_level->inode->ops.mkdir) {
190 errno = current_level->inode->ops.mkdir(
191 current_level->inode, dnode);
199 vfs_dcache_add(current_level, dnode);
201 dnode->parent = current_level;
202 llist_append(¤t_level->children, &dnode->siblings);
206 current_level = dnode;
211 *dentry = current_level;
215 vfree(dnode->name.value);
221 #define VFS_MAX_SYMLINK 16
224 vfs_walk(struct v_dnode* start,
226 struct v_dnode** dentry,
227 struct hstr* component,
230 struct v_dnode* interim;
231 char* pathname = path;
232 int errno = __vfs_walk(start, path, &interim, component, options);
236 if (counter >= VFS_MAX_SYMLINK) {
240 if ((interim->inode->itype & VFS_INODE_TYPE_SYMLINK) &&
241 !(options & VFS_WALK_NOFOLLOW) &&
242 interim->inode->ops.read_symlink) {
243 errno = interim->inode->ops.read_symlink(interim->inode, &pathname);
250 errno = __vfs_walk(start, pathname, &interim, component, options);
254 *dentry = errno ? 0 : interim;
260 vfs_mount(const char* target, const char* fs_name, bdev_t device)
265 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
266 errno = vfs_mount_at(fs_name, device, mnt);
273 vfs_unmount(const char* target)
278 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
279 errno = vfs_unmount_at(mnt);
286 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
288 struct filesystem* fs = fsm_get(fs_name);
291 struct v_superblock* sb = vfs_sb_alloc();
296 if (!(errno = fs->mount(sb, mnt_point))) {
298 sb->root = mnt_point;
299 mnt_point->super_block = sb;
300 llist_append(&root_sb->sb_list, &sb->sb_list);
307 vfs_unmount_at(struct v_dnode* mnt_point)
310 struct v_superblock* sb = mnt_point->super_block;
314 if (!(errno = sb->fs->unmount(sb))) {
315 struct v_dnode* fs_root = sb->root;
316 llist_delete(&fs_root->siblings);
317 llist_delete(&sb->sb_list);
324 vfs_open(struct v_dnode* dnode, struct v_file** file)
326 if (!dnode->inode || !dnode->inode->ops.open) {
330 struct v_file* vfile = cake_grab(file_pile);
331 memset(vfile, 0, sizeof(*vfile));
333 vfile->dnode = dnode;
334 vfile->inode = dnode->inode;
335 vfile->ref_count = 1;
336 dnode->inode->open_count++;
338 if ((dnode->inode->itype & VFS_INODE_TYPE_FILE) &&
339 !dnode->inode->pg_cache) {
340 struct pcache* pcache = vzalloc(sizeof(struct pcache));
342 dnode->inode->pg_cache = pcache;
345 int errno = dnode->inode->ops.open(dnode->inode, vfile);
347 cake_release(file_pile, vfile);
355 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
358 if (to_link->super_block->root != name->super_block->root) {
360 } else if (!to_link->inode->ops.link) {
362 } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
363 name->inode = to_link->inode;
364 to_link->inode->link_count++;
371 vfs_close(struct v_file* file)
374 if (!file->ops.close || !(errno = file->ops.close(file))) {
375 if (file->inode->open_count) {
376 file->inode->open_count--;
378 pcache_commit_all(file);
379 cake_release(file_pile, file);
385 vfs_fsync(struct v_file* file)
388 if (file->ops.sync) {
389 errno = file->ops.sync(file);
391 if (!errno && file->inode->ops.sync) {
392 return file->inode->ops.sync(file->inode);
398 vfs_alloc_fdslot(int* fd)
400 for (size_t i = 0; i < VFS_MAX_FD; i++) {
401 if (!__current->fdtable->fds[i]) {
412 struct v_superblock* sb = cake_grab(superblock_pile);
413 memset(sb, 0, sizeof(*sb));
414 llist_init_head(&sb->sb_list);
419 vfs_sb_free(struct v_superblock* sb)
421 cake_release(superblock_pile, sb);
427 struct v_dnode* dnode = cake_grab(dnode_pile);
428 memset(dnode, 0, sizeof(*dnode));
429 llist_init_head(&dnode->children);
430 dnode->name = vfs_empty;
435 vfs_d_free(struct v_dnode* dnode)
437 if (dnode->ops.destruct) {
438 dnode->ops.destruct(dnode);
440 cake_release(dnode_pile, dnode);
446 struct v_inode* inode = cake_grab(inode_pile);
447 memset(inode, 0, sizeof(*inode));
452 vfs_i_free(struct v_inode* inode)
454 cake_release(inode_pile, inode);
457 /* ---- System call definition and support ---- */
459 #define FLOCATE_CREATE_EMPTY 1
461 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
464 __vfs_try_locate_file(const char* path,
465 struct v_dnode** fdir,
466 struct v_dnode** file,
469 char name_str[VFS_NAME_MAXLEN];
470 struct hstr name = HSTR(name_str, 0);
472 if ((errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
476 errno = vfs_walk(*fdir, name.value, file, NULL, 0);
477 if (errno != ENOENT || !(options & FLOCATE_CREATE_EMPTY)) {
481 if (!(errno = (*fdir)->inode->ops.create((*fdir)->inode))) {
482 struct v_dnode* file_new;
483 file_new = vfs_d_alloc();
484 file_new->name = HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
485 strcpy(file_new->name.value, name_str);
488 llist_append(&(*fdir)->children, &file_new->siblings);
495 __vfs_do_open(struct v_file** file_out, const char* path, int options)
498 struct v_dnode *dentry, *file;
499 struct v_file* opened_file = 0;
501 errno = __vfs_try_locate_file(path, &dentry, &file, 0);
503 if (errno != ENOENT && (options & FO_CREATE)) {
504 errno = dentry->inode->ops.create(dentry->inode);
508 errno = vfs_open(file, &opened_file);
511 *file_out = opened_file;
515 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
517 struct v_file* opened_file;
518 int errno = __vfs_do_open(&opened_file, path, options), fd;
520 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
521 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
523 opened_file->inode->fsize & -((options & FO_APPEND) != 0);
524 fd_s->file = opened_file;
525 fd_s->flags = options;
526 __current->fdtable->fds[fd] = fd_s;
530 return DO_STATUS(errno);
532 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
533 #define GET_FD(fd, fd_s) (TEST_FD(fd) && (fd_s = __current->fdtable->fds[fd]))
535 __DEFINE_LXSYSCALL1(int, close, int, fd)
539 if (!GET_FD(fd, fd_s)) {
544 if (fd_s->file->ref_count > 1) {
545 fd_s->file->ref_count--;
546 } else if ((errno = vfs_close(fd_s->file))) {
551 __current->fdtable->fds[fd] = 0;
554 return DO_STATUS(errno);
558 __vfs_readdir_callback(struct dir_context* dctx,
563 struct dirent* dent = (struct dirent*)dctx->cb_data;
564 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
566 dent->d_type = dtype;
569 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
573 if (!GET_FD(fd, fd_s)) {
575 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
578 struct dir_context dctx =
579 (struct dir_context){ .cb_data = dent,
580 .index = dent->d_offset,
581 .read_complete_callback =
582 __vfs_readdir_callback };
583 if (dent->d_offset == 0) {
584 __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
585 } else if (dent->d_offset == 1) {
586 __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
589 if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
598 return DO_STATUS(errno);
601 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
603 struct v_dnode *parent, *dir;
604 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
605 int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
610 if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
612 } else if (!parent->inode->ops.mkdir) {
614 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
618 dir->name = component;
619 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
620 llist_append(&parent->children, &dir->siblings);
623 vfree(component.value);
628 return DO_STATUS(errno);
631 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
635 if (!GET_FD(fd, fd_s)) {
640 struct v_file* file = fd_s->file;
641 if ((file->inode->itype & VFS_INODE_TYPE_DIR)) {
646 if ((fd_s->flags & FO_DIRECT)) {
647 errno = file->ops.read(file, buf, count, file->f_pos);
649 errno = pcache_read(file, buf, count);
653 file->f_pos += errno;
658 return DO_STATUS(errno);
661 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
665 if (!GET_FD(fd, fd_s)) {
670 struct v_file* file = fd_s->file;
671 if ((file->inode->itype & VFS_INODE_TYPE_DIR)) {
676 if ((fd_s->flags & FO_DIRECT)) {
677 errno = file->ops.write(file, buf, count, file->f_pos);
679 errno = pcache_write(file, buf, count);
683 file->f_pos += errno;
688 return DO_STATUS(errno);
691 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
695 if (!GET_FD(fd, fd_s)) {
698 struct v_file* file = fd_s->file;
699 size_t fpos = file->f_pos;
702 fpos = (size_t)((int)file->f_pos + offset);
705 fpos = (size_t)((int)file->inode->fsize + offset);
711 if (!file->ops.seek || !(errno = file->ops.seek(file, fpos))) {
716 return DO_STATUS(errno);
720 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
730 size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
736 size_t cpy_size = MIN(dnode->name.len, size - len);
737 strncpy(buf + len, dnode->name.value, cpy_size);
741 buf[len++] = PATH_DELIM;
748 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
751 if (dnode->inode->ops.read_symlink) {
752 int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
753 strncpy(buf, link, size);
759 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
763 if (!GET_FD(fd, fd_s)) {
766 struct v_dnode* dnode;
767 errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
774 return DO_STATUS(errno);
777 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
780 struct v_dnode* dnode;
781 if (!(errno = vfs_walk(NULL, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
782 errno = vfs_readlink(dnode, buf, size);
789 return DO_STATUS(errno);
792 __DEFINE_LXSYSCALL4(int,
805 if (!GET_FD(dirfd, fd_s)) {
808 struct v_dnode* dnode;
809 if (!(errno = vfs_walk(fd_s->file->dnode,
813 VFS_WALK_NOFOLLOW))) {
814 errno = vfs_readlink(fd_s->file->dnode, buf, size);
822 return DO_STATUS(errno);
825 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
828 struct v_dnode* dnode;
829 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
832 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
837 if (dnode->inode->open_count) {
842 if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
843 errno = dnode->inode->ops.rmdir(dnode->inode);
849 return DO_STATUS(errno);
853 __vfs_do_unlink(struct v_inode* inode)
856 if (inode->open_count) {
858 } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
859 // TODO handle symbolic link and type other than regular file
860 errno = inode->ops.unlink(inode);
871 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
874 struct v_dnode* dnode;
875 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
878 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
883 errno = __vfs_do_unlink(dnode->inode);
886 return DO_STATUS(errno);
889 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
893 if (!GET_FD(fd, fd_s)) {
896 struct v_dnode* dnode;
897 if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
898 errno = __vfs_do_unlink(dnode->inode);
903 return DO_STATUS(errno);
906 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
909 struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
911 errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
913 errno = __vfs_try_locate_file(
914 newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
917 } else if (name_file) {
918 errno = vfs_link(to_link, name_file);
921 return DO_STATUS(errno);
924 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
928 if (!GET_FD(fildes, fd_s)) {
931 errno = vfs_fsync(fd_s->file);
934 return DO_STATUS(errno);
938 __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
941 struct v_fd* copied = cake_grab(fd_pile);
943 memcpy(copied, old, sizeof(struct v_fd));
944 old->file->ref_count++;
949 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
951 if (newfd == oldfd) {
956 struct v_fd *oldfd_s, *newfd_s;
957 if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
961 newfd_s = __current->fdtable->fds[newfd];
962 if (newfd_s && (errno = vfs_close(newfd_s))) {
966 if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
967 __current->fdtable->fds[newfd] = newfd_s;
972 return DO_STATUS(errno);
975 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
978 struct v_fd *oldfd_s, *newfd_s;
979 if (!GET_FD(oldfd, oldfd_s)) {
981 } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
982 !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
983 __current->fdtable->fds[newfd] = newfd_s;
987 return DO_STATUS(errno);
990 __DEFINE_LXSYSCALL2(int,
998 struct v_dnode* dnode;
999 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
1002 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
1006 if (!dnode->inode->ops.symlink) {
1011 errno = dnode->inode->ops.symlink(dnode->inode, link_target);
1014 return DO_STATUS(errno);