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);
251 __vfs_walk_internal(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(NULL, target, &mnt, NULL, 0))) {
267 errno = vfs_mount_at(fs_name, device, mnt);
274 vfs_unmount(const char* target)
279 if (!(errno = vfs_walk(NULL, 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_file* vfile = cake_grab(file_pile);
332 memset(vfile, 0, sizeof(*vfile));
334 vfile->dnode = dnode;
335 vfile->inode = dnode->inode;
336 dnode->inode->open_count++;
338 int errno = dnode->inode->ops.open(dnode->inode, vfile);
340 cake_release(file_pile, vfile);
348 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
351 if (to_link->super_block->root != name->super_block->root) {
353 } else if (!to_link->inode->ops.link) {
355 } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
356 name->inode = to_link->inode;
357 to_link->inode->link_count++;
364 vfs_close(struct v_file* file)
366 if (!file->ops.close) {
370 int errno = file->ops.close(file);
372 if (file->inode->open_count) {
373 file->inode->open_count--;
375 cake_release(file_pile, file);
381 vfs_fsync(struct v_file* file)
384 if (file->ops.sync) {
385 errno = file->ops.sync(file);
387 if (!errno && file->inode->ops.sync) {
388 return file->inode->ops.sync(file->inode);
394 vfs_alloc_fdslot(int* fd)
396 for (size_t i = 0; i < VFS_MAX_FD; i++) {
397 if (!__current->fdtable->fds[i]) {
408 struct v_superblock* sb = cake_grab(superblock_pile);
409 memset(sb, 0, sizeof(*sb));
410 llist_init_head(&sb->sb_list);
415 vfs_sb_free(struct v_superblock* sb)
417 cake_release(superblock_pile, sb);
423 struct v_dnode* dnode = cake_grab(dnode_pile);
424 memset(dnode, 0, sizeof(*dnode));
425 llist_init_head(&dnode->children);
426 dnode->name = vfs_empty;
431 vfs_d_free(struct v_dnode* dnode)
433 if (dnode->ops.destruct) {
434 dnode->ops.destruct(dnode);
436 cake_release(dnode_pile, dnode);
442 struct v_inode* inode = cake_grab(inode_pile);
443 memset(inode, 0, sizeof(*inode));
449 vfs_i_free(struct v_inode* inode)
451 cake_release(inode_pile, inode);
454 /* ---- System call definition and support ---- */
456 #define FLOCATE_CREATE_EMPTY 1
458 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
461 __vfs_try_locate_file(const char* path,
462 struct v_dnode** fdir,
463 struct v_dnode** file,
466 char name_str[VFS_NAME_MAXLEN];
467 struct hstr name = HSTR(name_str, 0);
469 if (!(errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
470 errno = vfs_walk(*fdir, name.value, file, NULL, 0);
471 if (errno == ENOENT && (options & FLOCATE_CREATE_EMPTY)) {
472 struct v_dnode* file_new;
473 file_new = vfs_d_alloc();
475 HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
476 strcpy(file_new->name.value, name_str);
479 llist_append(&(*fdir)->children, &file_new->siblings);
487 __vfs_do_open(struct v_file** file_out, const char* path, int options)
490 struct v_dnode *dentry, *file;
491 struct v_file* opened_file = 0;
493 errno = __vfs_try_locate_file(path, &dentry, &file, 0);
495 if (errno != ENOENT && (options & FO_CREATE)) {
496 errno = dentry->inode->ops.create(dentry->inode, opened_file);
498 errno = vfs_open(file, &opened_file);
501 *file_out = opened_file;
505 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
507 struct v_file* opened_file;
508 int errno = __vfs_do_open(&opened_file, path, options), fd;
510 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
511 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
512 fd_s->file = opened_file;
513 fd_s->pos = opened_file->inode->fsize & -((options & FO_APPEND) != 0);
514 __current->fdtable->fds[fd] = fd_s;
518 return DO_STATUS(errno);
520 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
521 #define GET_FD(fd, fd_s) (TEST_FD(fd) && (fd_s = __current->fdtable->fds[fd]))
523 __DEFINE_LXSYSCALL1(int, close, int, fd)
527 if (!GET_FD(fd, fd_s)) {
529 } else if (!(errno = vfs_close(fd_s->file))) {
531 __current->fdtable->fds[fd] = 0;
534 return DO_STATUS(errno);
538 __vfs_readdir_callback(struct dir_context* dctx,
543 struct dirent* dent = (struct dirent*)dctx->cb_data;
544 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
546 dent->d_type = dtype;
549 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
553 if (!GET_FD(fd, fd_s)) {
555 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
558 struct dir_context dctx =
559 (struct dir_context){ .cb_data = dent,
560 .index = dent->d_offset,
561 .read_complete_callback =
562 __vfs_readdir_callback };
563 if (dent->d_offset == 0) {
564 __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
565 } else if (dent->d_offset == 1) {
566 __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
569 if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
578 return DO_STATUS(errno);
581 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
583 struct v_dnode *parent, *dir;
584 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
585 int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
590 if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
592 } else if (!parent->inode->ops.mkdir) {
594 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
598 dir->name = component;
599 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
600 llist_append(&parent->children, &dir->siblings);
603 vfree(component.value);
608 return DO_STATUS(errno);
611 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
615 if (!GET_FD(fd, fd_s)) {
618 struct v_file* file = fd_s->file;
619 file->f_pos = fd_s->pos;
620 if ((errno = file->ops.read(file, buf, count)) >= 0) {
625 return DO_STATUS(errno);
628 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
632 if (!GET_FD(fd, fd_s)) {
635 struct v_file* file = fd_s->file;
636 file->f_pos = fd_s->pos;
637 if ((errno = file->ops.write(file, buf, count)) >= 0) {
642 return DO_STATUS(errno);
645 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
649 if (!GET_FD(fd, fd_s)) {
652 size_t fpos = fd_s->file->f_pos;
655 fpos = (size_t)((int)fd_s->file->f_pos + offset);
658 fpos = (size_t)((int)fd_s->file->inode->fsize + offset);
670 return DO_STATUS(errno);
674 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
684 size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
690 size_t cpy_size = MIN(dnode->name.len, size - len);
691 strncpy(buf + len, dnode->name.value, cpy_size);
695 buf[len++] = PATH_DELIM;
702 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
705 if (dnode->inode->ops.read_symlink) {
706 int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
707 strncpy(buf, link, size);
713 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
717 if (!GET_FD(fd, fd_s)) {
720 struct v_dnode* dnode;
721 errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
728 return DO_STATUS(errno);
731 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
734 struct v_dnode* dnode;
735 if (!(errno = vfs_walk(NULL, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
736 errno = vfs_readlink(dnode, buf, size);
743 return DO_STATUS(errno);
746 __DEFINE_LXSYSCALL4(int,
759 if (!GET_FD(dirfd, fd_s)) {
762 struct v_dnode* dnode;
763 if (!(errno = vfs_walk(fd_s->file->dnode,
767 VFS_WALK_NOFOLLOW))) {
768 errno = vfs_readlink(fd_s->file->dnode, buf, size);
776 return DO_STATUS(errno);
779 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
782 struct v_dnode* dnode;
783 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
786 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
791 if (dnode->inode->open_count) {
796 if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
797 errno = dnode->inode->ops.rmdir(dnode->inode);
803 return DO_STATUS(errno);
807 __vfs_do_unlink(struct v_inode* inode)
810 if (inode->open_count) {
812 } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
813 // TODO handle symbolic link and type other than regular file
814 errno = inode->ops.unlink(inode);
825 __DEFINE_LXSYSCALL1(int, unlink, 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 errno = __vfs_do_unlink(dnode->inode);
840 return DO_STATUS(errno);
843 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
847 if (!GET_FD(fd, fd_s)) {
850 struct v_dnode* dnode;
851 if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
852 errno = __vfs_do_unlink(dnode->inode);
857 return DO_STATUS(errno);
860 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
863 struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
865 errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
867 errno = __vfs_try_locate_file(
868 newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
871 } else if (name_file) {
872 errno = vfs_link(to_link, name_file);
875 return DO_STATUS(errno);
878 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
882 if (!GET_FD(fildes, fd_s)) {
885 errno = vfs_fsync(fd_s->file);
888 return DO_STATUS(errno);
892 __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
895 struct v_file* newopened;
896 if (!(errno = vfs_open(old->file->dnode, &newopened))) {
897 *new = cake_grab(fd_pile);
898 **new = (struct v_fd){ .file = newopened,
900 .flags = old->flags };
906 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
908 if (newfd == oldfd) {
913 struct v_fd *oldfd_s, *newfd_s;
914 if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
918 newfd_s = __current->fdtable->fds[newfd];
919 if (newfd_s && (errno = vfs_close(newfd_s))) {
923 if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
924 __current->fdtable->fds[newfd] = newfd_s;
929 return DO_STATUS(errno);
932 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
935 struct v_fd *oldfd_s, *newfd_s;
936 if (!GET_FD(oldfd, oldfd_s)) {
938 } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
939 !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
940 __current->fdtable->fds[newfd] = newfd_s;
944 return DO_STATUS(errno);
947 __DEFINE_LXSYSCALL2(int,
955 struct v_dnode* dnode;
956 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
959 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
963 if (!dnode->inode->ops.symlink) {
968 errno = dnode->inode->ops.symlink(dnode->inode, link_target);
971 return DO_STATUS(errno);