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();
75 root_sb->root->inode = vfs_i_alloc();
78 inline struct hbucket*
79 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
81 // 与parent的指针值做加法,来减小碰撞的可能性。
82 hash += (uint32_t)parent;
84 hash = hash ^ (hash >> DNODE_HASHBITS);
85 return &dnode_cache[hash & DNODE_HASH_MASK];
89 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
91 if (!str->len || HSTR_EQ(str, &vfs_dot))
94 if (HSTR_EQ(str, &vfs_ddot)) {
95 return parent->parent ? parent->parent : parent;
98 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
100 struct v_dnode *pos, *n;
101 hashtable_bucket_foreach(slot, pos, n, hash_list)
103 if (pos->name.hash == str->hash) {
111 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
113 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
114 hlist_add(&bucket->head, &dnode->hash_list);
118 __vfs_walk(struct v_dnode* start,
120 struct v_dnode** dentry,
121 struct hstr* component,
127 if (path[0] == PATH_DELIM || !start) {
128 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
129 start = start->super_block->root;
131 start = root_sb->root;
136 struct v_dnode* dnode;
137 struct v_dnode* current_level = start;
139 char name_content[VFS_NAME_MAXLEN];
140 struct hstr name = HSTR(name_content, 0);
142 char current = path[i++], lookahead;
144 lookahead = path[i++];
145 if (current != PATH_DELIM) {
146 if (j >= VFS_NAME_MAXLEN - 1) {
149 if (!VFS_VALID_CHAR(current)) {
152 name_content[j++] = current;
158 // handling cases like /^.*(\/+).*$/
159 if (lookahead == PATH_DELIM) {
165 hstr_rehash(&name, HSTR_FULL_HASH);
167 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
169 component->hash = name.hash;
171 strcpy(component->value, name_content);
176 dnode = vfs_dcache_lookup(current_level, &name);
179 dnode = vfs_d_alloc();
180 dnode->name = HHSTR(valloc(VFS_NAME_MAXLEN), j, name.hash);
182 strcpy(dnode->name.value, name_content);
185 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
187 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
188 if (!current_level->inode->ops.mkdir) {
191 errno = current_level->inode->ops.mkdir(
192 current_level->inode, dnode);
200 vfs_dcache_add(current_level, dnode);
202 dnode->parent = current_level;
203 llist_append(¤t_level->children, &dnode->siblings);
207 current_level = dnode;
212 *dentry = current_level;
216 vfree(dnode->name.value);
222 #define VFS_MAX_SYMLINK 16
225 vfs_walk(struct v_dnode* start,
227 struct v_dnode** dentry,
228 struct hstr* component,
231 struct v_dnode* interim;
232 char* pathname = path;
233 int errno = __vfs_walk(start, path, &interim, component, options);
237 if (counter >= VFS_MAX_SYMLINK) {
241 if ((interim->inode->itype & VFS_IFSYMLINK) &&
242 !(options & VFS_WALK_NOFOLLOW) &&
243 interim->inode->ops.read_symlink) {
244 errno = interim->inode->ops.read_symlink(interim->inode, &pathname);
251 errno = __vfs_walk(start, pathname, &interim, component, options);
255 *dentry = errno ? 0 : interim;
261 vfs_mount(const char* target, const char* fs_name, bdev_t device)
266 if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
267 errno = vfs_mount_at(fs_name, device, mnt);
274 vfs_unmount(const char* target)
279 if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
280 errno = vfs_unmount_at(mnt);
287 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
289 struct filesystem* fs = fsm_get(fs_name);
292 struct v_superblock* sb = vfs_sb_alloc();
297 if (!(errno = fs->mount(sb, mnt_point))) {
299 sb->root = mnt_point;
300 mnt_point->super_block = sb;
301 llist_append(&root_sb->sb_list, &sb->sb_list);
308 vfs_unmount_at(struct v_dnode* mnt_point)
311 struct v_superblock* sb = mnt_point->super_block;
315 if (!(errno = sb->fs->unmount(sb))) {
316 struct v_dnode* fs_root = sb->root;
317 llist_delete(&fs_root->siblings);
318 llist_delete(&sb->sb_list);
325 vfs_open(struct v_dnode* dnode, struct v_file** file)
327 if (!dnode->inode || !dnode->inode->ops.open) {
331 struct v_inode* inode = dnode->inode;
332 struct v_file* vfile = cake_grab(file_pile);
333 memset(vfile, 0, sizeof(*vfile));
335 vfile->dnode = dnode;
336 vfile->inode = inode;
337 vfile->ref_count = 1;
338 vfile->ops = inode->default_fops;
340 if ((inode->itype & VFS_IFFILE) && !inode->pg_cache) {
341 struct pcache* pcache = vzalloc(sizeof(struct pcache));
343 inode->pg_cache = pcache;
346 int errno = inode->ops.open(inode, vfile);
348 cake_release(file_pile, vfile);
359 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
362 if (to_link->super_block->root != name->super_block->root) {
364 } else if (!to_link->inode->ops.link) {
366 } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
367 name->inode = to_link->inode;
368 to_link->inode->link_count++;
375 vfs_close(struct v_file* file)
378 if (!file->ops.close || !(errno = file->ops.close(file))) {
379 file->dnode->ref_count--;
380 file->inode->open_count--;
381 pcache_commit_all(file);
382 cake_release(file_pile, file);
388 vfs_fsync(struct v_file* file)
391 pcache_commit_all(file);
392 if (file->ops.sync) {
393 errno = file->ops.sync(file);
395 if (!errno && file->inode->ops.sync) {
396 return file->inode->ops.sync(file->inode);
402 vfs_alloc_fdslot(int* fd)
404 for (size_t i = 0; i < VFS_MAX_FD; i++) {
405 if (!__current->fdtable->fds[i]) {
416 struct v_superblock* sb = cake_grab(superblock_pile);
417 memset(sb, 0, sizeof(*sb));
418 llist_init_head(&sb->sb_list);
423 vfs_sb_free(struct v_superblock* sb)
425 cake_release(superblock_pile, sb);
431 struct v_dnode* dnode = cake_grab(dnode_pile);
432 memset(dnode, 0, sizeof(*dnode));
433 llist_init_head(&dnode->children);
434 dnode->name = vfs_empty;
439 vfs_d_free(struct v_dnode* dnode)
441 if (dnode->ops.destruct) {
442 dnode->ops.destruct(dnode);
444 cake_release(dnode_pile, dnode);
450 struct v_inode* inode = cake_grab(inode_pile);
451 memset(inode, 0, sizeof(*inode));
456 vfs_i_free(struct v_inode* inode)
458 cake_release(inode_pile, inode);
461 /* ---- System call definition and support ---- */
463 #define FLOCATE_CREATE_EMPTY 1
465 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
466 #define DO_STATUS_OR_RETURN(errno) ({ errno < 0 ? DO_STATUS(errno) : errno; })
468 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
471 __vfs_getfd(int fd, struct v_fd** fd_s)
473 if (TEST_FD(fd) && (*fd_s = __current->fdtable->fds[fd])) {
480 __vfs_try_locate_file(const char* path,
481 struct v_dnode** fdir,
482 struct v_dnode** file,
485 char name_str[VFS_NAME_MAXLEN];
486 struct hstr name = HSTR(name_str, 0);
489 vfs_walk(__current->cwd, path, fdir, &name, VFS_WALK_PARENT))) {
493 errno = vfs_walk(*fdir, name.value, file, NULL, 0);
494 if (errno != ENOENT || !(options & FLOCATE_CREATE_EMPTY)) {
498 if (!(errno = (*fdir)->inode->ops.create((*fdir)->inode))) {
499 struct v_dnode* file_new;
500 file_new = vfs_d_alloc();
501 file_new->name = HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
502 strcpy(file_new->name.value, name_str);
505 llist_append(&(*fdir)->children, &file_new->siblings);
512 vfs_do_open(const char* path, int options)
515 struct v_dnode *dentry, *file;
516 struct v_file* ofile = 0;
518 errno = __vfs_try_locate_file(path, &dentry, &file, 0);
520 if (errno != ENOENT && (options & FO_CREATE)) {
521 errno = dentry->inode->ops.create(dentry->inode);
524 if (!errno && (errno = vfs_open(file, &ofile))) {
528 struct v_inode* o_inode = ofile->inode;
529 if (!(o_inode->itype & VFS_IFSEQDEV) && !(options & FO_DIRECT)) {
530 // XXX Change here accordingly when signature of pcache_r/w changed.
531 ofile->ops.read = pcache_read;
532 ofile->ops.write = pcache_write;
535 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
536 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
537 ofile->f_pos = ofile->inode->fsize & -((options & FO_APPEND) != 0);
539 fd_s->flags = options;
540 __current->fdtable->fds[fd] = fd_s;
547 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
549 int errno = vfs_do_open(path, options);
550 return DO_STATUS_OR_RETURN(errno);
553 __DEFINE_LXSYSCALL1(int, close, int, fd)
557 if ((errno = __vfs_getfd(fd, &fd_s))) {
561 if (fd_s->file->ref_count > 1) {
562 fd_s->file->ref_count--;
563 } else if ((errno = vfs_close(fd_s->file))) {
568 __current->fdtable->fds[fd] = 0;
571 return DO_STATUS(errno);
575 __vfs_readdir_callback(struct dir_context* dctx,
580 struct dirent* dent = (struct dirent*)dctx->cb_data;
581 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
583 dent->d_type = dtype;
586 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
591 if ((errno = __vfs_getfd(fd, &fd_s))) {
595 if (!(fd_s->file->inode->itype & VFS_IFDIR)) {
598 struct dir_context dctx =
599 (struct dir_context){ .cb_data = dent,
600 .index = dent->d_offset,
601 .read_complete_callback =
602 __vfs_readdir_callback };
603 if (dent->d_offset == 0) {
604 __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
605 } else if (dent->d_offset == 1) {
606 __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
609 if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
618 return DO_STATUS(errno);
621 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
623 struct v_dnode *parent, *dir;
624 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
626 vfs_walk(__current->cwd, path, &parent, &component, VFS_WALK_PARENT);
631 if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
633 } else if (!parent->inode->ops.mkdir) {
635 } else if (!(parent->inode->itype & VFS_IFDIR)) {
639 dir->name = component;
640 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
641 llist_append(&parent->children, &dir->siblings);
644 vfree(component.value);
649 return DO_STATUS(errno);
652 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
656 if ((errno = __vfs_getfd(fd, &fd_s))) {
660 struct v_file* file = fd_s->file;
661 if ((file->inode->itype & VFS_IFDIR)) {
666 cpu_enable_interrupt();
667 errno = file->ops.read(file, buf, count, file->f_pos);
668 cpu_disable_interrupt();
671 file->f_pos += errno;
676 return DO_STATUS(errno);
679 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
683 if ((errno = __vfs_getfd(fd, &fd_s))) {
687 struct v_file* file = fd_s->file;
688 if ((file->inode->itype & VFS_IFDIR)) {
693 cpu_enable_interrupt();
694 errno = file->ops.write(file, buf, count, file->f_pos);
695 cpu_disable_interrupt();
698 file->f_pos += errno;
703 return DO_STATUS(errno);
706 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
710 if ((errno = __vfs_getfd(fd, &fd_s))) {
714 struct v_file* file = fd_s->file;
715 size_t fpos = file->f_pos;
718 fpos = (size_t)((int)file->f_pos + offset);
721 fpos = (size_t)((int)file->inode->fsize + offset);
727 if (!file->ops.seek || !(errno = file->ops.seek(file, fpos))) {
732 return DO_STATUS(errno);
736 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
746 size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
752 size_t cpy_size = MIN(dnode->name.len, size - len);
753 strncpy(buf + len, dnode->name.value, cpy_size);
757 buf[len++] = PATH_DELIM;
764 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
767 if (dnode->inode->ops.read_symlink) {
768 int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
769 strncpy(buf, link, size);
775 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
779 if ((errno = __vfs_getfd(fd, &fd_s))) {
783 struct v_dnode* dnode;
784 errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
791 return DO_STATUS(errno);
794 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
797 struct v_dnode* dnode;
799 vfs_walk(__current->cwd, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
800 errno = vfs_readlink(dnode, buf, size);
807 return DO_STATUS(errno);
810 __DEFINE_LXSYSCALL4(int,
823 if ((errno = __vfs_getfd(dirfd, &fd_s))) {
827 struct v_dnode* dnode;
828 if (!(errno = vfs_walk(
829 fd_s->file->dnode, pathname, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
830 errno = vfs_readlink(fd_s->file->dnode, buf, size);
838 return DO_STATUS(errno);
841 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
844 struct v_dnode* dnode;
845 if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
848 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
853 if (dnode->inode->open_count) {
858 if ((dnode->inode->itype & VFS_IFDIR)) {
859 errno = dnode->inode->ops.rmdir(dnode->inode);
865 return DO_STATUS(errno);
869 __vfs_do_unlink(struct v_inode* inode)
872 if (inode->open_count) {
874 } else if (!(inode->itype & VFS_IFDIR)) {
875 // TODO handle symbolic link and type other than regular file
876 errno = inode->ops.unlink(inode);
887 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
890 struct v_dnode* dnode;
891 if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
894 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
899 errno = __vfs_do_unlink(dnode->inode);
902 return DO_STATUS(errno);
905 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
909 if ((errno = __vfs_getfd(fd, &fd_s))) {
913 struct v_dnode* dnode;
914 if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
915 errno = __vfs_do_unlink(dnode->inode);
919 return DO_STATUS(errno);
922 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
925 struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
927 errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
929 errno = __vfs_try_locate_file(
930 newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
933 } else if (name_file) {
934 errno = vfs_link(to_link, name_file);
937 return DO_STATUS(errno);
940 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
944 if (!(errno = __vfs_getfd(fildes, &fd_s))) {
945 errno = vfs_fsync(fd_s->file);
948 return DO_STATUS(errno);
952 vfs_dup_fd(struct v_fd* old, struct v_fd** new)
955 struct v_fd* copied = cake_grab(fd_pile);
957 memcpy(copied, old, sizeof(struct v_fd));
958 old->file->ref_count++;
966 vfs_dup2(int oldfd, int newfd)
968 if (newfd == oldfd) {
973 struct v_fd *oldfd_s, *newfd_s;
974 if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
978 if (!TEST_FD(newfd)) {
983 newfd_s = __current->fdtable->fds[newfd];
984 if (newfd_s && (errno = vfs_close(newfd_s))) {
988 if (!(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
989 __current->fdtable->fds[newfd] = newfd_s;
994 return DO_STATUS(errno);
997 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
999 return vfs_dup2(oldfd, newfd);
1002 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
1005 struct v_fd *oldfd_s, *newfd_s;
1006 if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
1010 if (!(errno = vfs_alloc_fdslot(&newfd)) &&
1011 !(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
1012 __current->fdtable->fds[newfd] = newfd_s;
1017 return DO_STATUS(errno);
1020 __DEFINE_LXSYSCALL2(int,
1028 struct v_dnode* dnode;
1029 if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
1032 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
1036 if (!dnode->inode->ops.symlink) {
1041 errno = dnode->inode->ops.symlink(dnode->inode, link_target);
1044 return DO_STATUS(errno);
1048 __vfs_do_chdir(struct v_dnode* dnode)
1051 if (!(dnode->inode->itype & VFS_IFDIR)) {
1056 if (__current->cwd) {
1057 __current->cwd->ref_count--;
1061 __current->cwd = dnode;
1067 __DEFINE_LXSYSCALL1(int, chdir, const char*, path)
1069 struct v_dnode* dnode;
1072 if ((errno = vfs_walk(__current->cwd, path, &dnode, NULL, 0))) {
1076 errno = __vfs_do_chdir(dnode);
1079 return DO_STATUS(errno);
1082 __DEFINE_LXSYSCALL1(int, fchdir, int, fd)
1087 if ((errno = __vfs_getfd(fd, &fd_s))) {
1091 errno = __vfs_do_chdir(fd_s->file->dnode);
1094 return DO_STATUS(errno);
1097 __DEFINE_LXSYSCALL2(char*, getcwd, char*, buf, size_t, size)
1106 if (!__current->cwd) {
1111 size_t len = vfs_get_path(__current->cwd, buf, size, 0);
1117 buf[len + 1] = '\0';
1122 __current->k_status = errno;