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);
222 vfs_mount(const char* target, const char* fs_name, bdev_t device)
227 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
228 errno = vfs_mount_at(fs_name, device, mnt);
235 vfs_unmount(const char* target)
240 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
241 errno = vfs_unmount_at(mnt);
248 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
250 struct filesystem* fs = fsm_get(fs_name);
253 struct v_superblock* sb = vfs_sb_alloc();
258 if (!(errno = fs->mount(sb, mnt_point))) {
260 sb->root = mnt_point;
261 mnt_point->super_block = sb;
262 llist_append(&root_sb->sb_list, &sb->sb_list);
269 vfs_unmount_at(struct v_dnode* mnt_point)
272 struct v_superblock* sb = mnt_point->super_block;
276 if (!(errno = sb->fs->unmount(sb))) {
277 struct v_dnode* fs_root = sb->root;
278 llist_delete(&fs_root->siblings);
279 llist_delete(&sb->sb_list);
286 vfs_open(struct v_dnode* dnode, struct v_file** file)
288 if (!dnode->inode || !dnode->inode->ops.open) {
292 struct v_file* vfile = cake_grab(file_pile);
293 memset(vfile, 0, sizeof(*vfile));
295 vfile->dnode = dnode;
296 vfile->inode = dnode->inode;
297 dnode->inode->open_count++;
299 int errno = dnode->inode->ops.open(dnode->inode, vfile);
301 cake_release(file_pile, vfile);
309 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
312 if (to_link->super_block->root != name->super_block->root) {
314 } else if (!to_link->inode->ops.link) {
316 } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
317 name->inode = to_link->inode;
318 to_link->inode->link_count++;
325 vfs_close(struct v_file* file)
327 if (!file->ops.close) {
331 int errno = file->ops.close(file);
333 if (file->inode->open_count) {
334 file->inode->open_count--;
336 cake_release(file_pile, file);
342 vfs_fsync(struct v_file* file)
345 if (file->ops.sync) {
346 errno = file->ops.sync(file);
348 if (!errno && file->inode->ops.sync) {
349 return file->inode->ops.sync(file->inode);
355 vfs_alloc_fdslot(int* fd)
357 for (size_t i = 0; i < VFS_MAX_FD; i++) {
358 if (!__current->fdtable->fds[i]) {
369 struct v_superblock* sb = cake_grab(superblock_pile);
370 memset(sb, 0, sizeof(*sb));
371 llist_init_head(&sb->sb_list);
376 vfs_sb_free(struct v_superblock* sb)
378 cake_release(superblock_pile, sb);
384 struct v_dnode* dnode = cake_grab(dnode_pile);
385 memset(dnode, 0, sizeof(*dnode));
386 llist_init_head(&dnode->children);
387 dnode->name = vfs_empty;
392 vfs_d_free(struct v_dnode* dnode)
394 if (dnode->ops.destruct) {
395 dnode->ops.destruct(dnode);
397 cake_release(dnode_pile, dnode);
403 struct v_inode* inode = cake_grab(inode_pile);
404 memset(inode, 0, sizeof(*inode));
410 vfs_i_free(struct v_inode* inode)
412 cake_release(inode_pile, inode);
415 /* ---- System call definition and support ---- */
417 #define FLOCATE_CREATE_EMPTY 1
419 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
422 __vfs_try_locate_file(const char* path,
423 struct v_dnode** fdir,
424 struct v_dnode** file,
427 char name_str[VFS_NAME_MAXLEN];
428 struct hstr name = HSTR(name_str, 0);
430 if (!(errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
431 errno = vfs_walk(*fdir, name.value, file, NULL, 0);
432 if (errno == ENOENT && (options & FLOCATE_CREATE_EMPTY)) {
433 struct v_dnode* file_new;
434 file_new = vfs_d_alloc();
436 HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
437 strcpy(file_new->name.value, name_str);
440 llist_append(&(*fdir)->children, &file_new->siblings);
448 __vfs_do_open(struct v_file** file_out, const char* path, int options)
451 struct v_dnode *dentry, *file;
452 struct v_file* opened_file = 0;
454 errno = __vfs_try_locate_file(path, &dentry, &file, 0);
456 if (errno != ENOENT && (options & FO_CREATE)) {
457 errno = dentry->inode->ops.create(dentry->inode, opened_file);
459 errno = vfs_open(file, &opened_file);
462 *file_out = opened_file;
466 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
468 struct v_file* opened_file;
469 int errno = __vfs_do_open(&opened_file, path, options), fd;
471 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
472 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
473 fd_s->file = opened_file;
474 fd_s->pos = opened_file->inode->fsize & -((options & FO_APPEND) != 0);
475 __current->fdtable->fds[fd] = fd_s;
479 return DO_STATUS(errno);
482 #define GET_FD(fd, fd_s) \
483 (fd >= 0 && fd < VFS_MAX_FD && (fd_s = __current->fdtable->fds[fd]))
485 __DEFINE_LXSYSCALL1(int, close, int, fd)
489 if (!GET_FD(fd, fd_s)) {
491 } else if (!(errno = vfs_close(fd_s->file))) {
493 __current->fdtable->fds[fd] = 0;
496 return DO_STATUS(errno);
500 __vfs_readdir_callback(struct dir_context* dctx,
505 struct dirent* dent = (struct dirent*)dctx->cb_data;
506 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
508 dent->d_type = dtype;
511 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
515 if (!GET_FD(fd, fd_s)) {
517 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
520 struct dir_context dctx =
521 (struct dir_context){ .cb_data = dent,
522 .index = dent->d_offset,
523 .read_complete_callback =
524 __vfs_readdir_callback };
525 if (dent->d_offset == 0) {
526 __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
527 } else if (dent->d_offset == 1) {
528 __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
531 if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
540 return DO_STATUS(errno);
543 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
545 struct v_dnode *parent, *dir;
546 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
547 int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
552 if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
554 } else if (!parent->inode->ops.mkdir) {
556 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
560 dir->name = component;
561 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
562 llist_append(&parent->children, &dir->siblings);
565 vfree(component.value);
570 return DO_STATUS(errno);
573 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
577 if (!GET_FD(fd, fd_s)) {
580 struct v_file* file = fd_s->file;
581 file->f_pos = fd_s->pos;
582 if ((errno = file->ops.read(file, buf, count)) >= 0) {
587 return DO_STATUS(errno);
590 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
594 if (!GET_FD(fd, fd_s)) {
597 struct v_file* file = fd_s->file;
598 file->f_pos = fd_s->pos;
599 if ((errno = file->ops.write(file, buf, count)) >= 0) {
604 return DO_STATUS(errno);
607 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
611 if (!GET_FD(fd, fd_s)) {
614 size_t fpos = fd_s->file->f_pos;
617 fpos = (size_t)((int)fd_s->file->f_pos + offset);
620 fpos = (size_t)((int)fd_s->file->inode->fsize + offset);
632 return DO_STATUS(errno);
636 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size, int depth)
646 size_t len = vfs_readlink(dnode->parent, buf, size, depth + 1);
652 size_t cpy_size = MIN(dnode->name.len, size - len);
653 strncpy(buf + len, dnode->name.value, cpy_size);
657 buf[len++] = PATH_DELIM;
663 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
666 struct v_dnode* dnode;
667 if (!(errno = vfs_walk(NULL, path, &dnode, NULL, 0))) {
668 errno = vfs_readlink(dnode, buf, size, 0);
675 return DO_STATUS(errno);
678 __DEFINE_LXSYSCALL4(int,
691 if (!GET_FD(dirfd, fd_s)) {
694 struct v_dnode* dnode;
695 if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
696 errno = vfs_readlink(fd_s->file->dnode, buf, size, 0);
704 return DO_STATUS(errno);
707 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
710 struct v_dnode* dnode;
711 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
714 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
719 if (dnode->inode->open_count) {
724 if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
725 errno = dnode->inode->ops.rmdir(dnode->inode);
731 return DO_STATUS(errno);
735 __vfs_do_unlink(struct v_inode* inode)
738 if (inode->open_count) {
740 } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
741 // TODO handle symbolic link and type other than regular file
742 errno = inode->ops.unlink(inode);
753 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
756 struct v_dnode* dnode;
757 if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
760 if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
765 errno = __vfs_do_unlink(dnode->inode);
768 return DO_STATUS(errno);
771 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
775 if (!GET_FD(fd, fd_s)) {
778 struct v_dnode* dnode;
779 if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
780 errno = __vfs_do_unlink(dnode->inode);
785 return DO_STATUS(errno);
788 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
791 struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
793 errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
795 errno = __vfs_try_locate_file(
796 newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
799 } else if (name_file) {
800 errno = vfs_link(to_link, name_file);
803 return DO_STATUS(errno);
806 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
810 if (!GET_FD(fildes, fd_s)) {
813 errno = vfs_fsync(fd_s->file);
816 return DO_STATUS(errno);