From 90129c6876c9bac2e468ab82c8264352d1952b4d Mon Sep 17 00:00:00 2001 From: Minep Date: Sat, 30 Jul 2022 17:12:54 +0100 Subject: [PATCH 1/1] feat: implement readlink(2) readlinkat(2) refactor: pull the . and .. support to vfs level chore: refactor and bug fixes --- README.md | 2 + lunaix-os/includes/lunaix/fctrl.h | 14 ++++ lunaix-os/includes/lunaix/fs.h | 1 + lunaix-os/includes/lunaix/status.h | 1 + lunaix-os/includes/lunaix/syscall.h | 4 +- lunaix-os/kernel/asm/x86/syscall.S | 2 + lunaix-os/kernel/demos/dir_read.c | 16 ++++- lunaix-os/kernel/fs/twifs.c | 25 +------ lunaix-os/kernel/fs/vfs.c | 102 ++++++++++++++++++++++++++-- lunaix-os/kernel/proc0.c | 4 +- 10 files changed, 138 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8e35534..b995657 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ qemu-img create -f vdi machine/disk1.vdi 128M 2. `mkdir(2)` 2. `lseek(2)` 2. `readdir(2)` +2. `readlink(2)` +2. `readlinkat(2)` ### LunaixOS自有 diff --git a/lunaix-os/includes/lunaix/fctrl.h b/lunaix-os/includes/lunaix/fctrl.h index e76ae7b..0cd2705 100644 --- a/lunaix-os/includes/lunaix/fctrl.h +++ b/lunaix-os/includes/lunaix/fctrl.h @@ -3,6 +3,7 @@ #include #include +#include __LXSYSCALL2(int, open, const char*, path, int, options); @@ -18,4 +19,17 @@ __LXSYSCALL3(int, read, int, fd, void*, buf, unsigned int, count); __LXSYSCALL3(int, write, int, fd, void*, buf, unsigned int, count); +__LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size); + +__LXSYSCALL4(int, + readlinkat, + int, + dirfd, + const char*, + pathname, + char*, + buf, + size_t, + size); + #endif /* __LUNAIX_FCTRL_H */ diff --git a/lunaix-os/includes/lunaix/fs.h b/lunaix-os/includes/lunaix/fs.h index fe9b39b..7f05413 100644 --- a/lunaix-os/includes/lunaix/fs.h +++ b/lunaix-os/includes/lunaix/fs.h @@ -89,6 +89,7 @@ struct v_file_ops struct v_file { struct v_inode* inode; + struct v_dnode* dnode; struct llist_header* f_list; uint32_t f_pos; void* data; // 允许底层FS绑定他的一些专有数据 diff --git a/lunaix-os/includes/lunaix/status.h b/lunaix-os/includes/lunaix/status.h index 2c3ff96..17b509e 100644 --- a/lunaix-os/includes/lunaix/status.h +++ b/lunaix-os/includes/lunaix/status.h @@ -18,5 +18,6 @@ #define EBADF -13 #define ENOTSUP -14 #define ENXIO -15 +#define ELOOP -16 #endif /* __LUNAIX_CODE_H */ diff --git a/lunaix-os/includes/lunaix/syscall.h b/lunaix-os/includes/lunaix/syscall.h index 88aa026..f5b72d3 100644 --- a/lunaix-os/includes/lunaix/syscall.h +++ b/lunaix-os/includes/lunaix/syscall.h @@ -30,6 +30,8 @@ #define __SYSCALL_mkdir 24 #define __SYSCALL_lseek 25 #define __SYSCALL_geterrno 26 +#define __SYSCALL_readlink 27 +#define __SYSCALL_readlinkat 28 #define __SYSCALL_MAX 0x100 @@ -102,7 +104,7 @@ syscall_install(); } #define __LXSYSCALL4(rettype, name, t1, p1, t2, p2, t3, p3, t4, p4) \ - static rettype name(__PARAM_MAP3(t1, p1, t2, p2, t3, p3, t4, p4)) \ + static rettype name(__PARAM_MAP4(t1, p1, t2, p2, t3, p3, t4, p4)) \ { \ asm("\n" ::"b"(p1), "c"(p2), "d"(p3), "D"(p4)); \ ___DOINT33(__SYSCALL_##name, rettype) \ diff --git a/lunaix-os/kernel/asm/x86/syscall.S b/lunaix-os/kernel/asm/x86/syscall.S index 6a8b432..61b9879 100644 --- a/lunaix-os/kernel/asm/x86/syscall.S +++ b/lunaix-os/kernel/asm/x86/syscall.S @@ -34,6 +34,8 @@ .long __lxsys_mkdir .long __lxsys_lseek /* 25 */ .long __lxsys_geterrno + .long __lxsys_readlink + .long __lxsys_readlinkat 2: .rept __SYSCALL_MAX - (2b - 1b)/4 .long 0 diff --git a/lunaix-os/kernel/demos/dir_read.c b/lunaix-os/kernel/demos/dir_read.c index 7a63ba9..a0ef65f 100644 --- a/lunaix-os/kernel/demos/dir_read.c +++ b/lunaix-os/kernel/demos/dir_read.c @@ -1,5 +1,6 @@ #include #include +#include #include LOG_MODULE("RDDIR") @@ -7,9 +8,9 @@ LOG_MODULE("RDDIR") void _readdir_main() { - int fd = open("/dev/block", 0); + int fd = open("/dev/./../dev/.", 0); if (fd == -1) { - kprintf(KERROR "fail to open\n"); + kprintf(KERROR "fail to open (%d)\n", geterrno()); return; } @@ -19,6 +20,17 @@ _readdir_main() kprintf(KINFO "%s\n", ent.d_name); } + char path[129]; + + int len = readlinkat(fd, ".", path, 128); + if (len < 0) { + kprintf(KERROR "fail to read (%d)\n", geterrno()); + } + + path[len] = 0; + + kprintf("%s\n", path); + close(fd); return; diff --git a/lunaix-os/kernel/fs/twifs.c b/lunaix-os/kernel/fs/twifs.c index 37af44e..104a2fb 100644 --- a/lunaix-os/kernel/fs/twifs.c +++ b/lunaix-os/kernel/fs/twifs.c @@ -74,14 +74,8 @@ __twifs_new_node(struct twifs_node* parent, const char* name, int name_len) void twifs_rm_node(struct twifs_node* node) { - // TODO recursivly delete any sub-directories. if ((node->itype & VFS_INODE_TYPE_DIR)) { - struct twifs_node* dir = __twifs_get_node(node, &vfs_dot); - struct twifs_node* dir2 = __twifs_get_node(node, &vfs_ddot); - vfs_i_free(dir->inode); - vfs_i_free(dir2->inode); - cake_release(twi_pile, dir); - cake_release(twi_pile, dir2); + // TODO recursivly delete any sub-directories. } llist_delete(&node->siblings); vfs_i_free(node->inode); @@ -112,18 +106,10 @@ twifs_dir_node(struct twifs_node* parent, const char* name, int name_len) struct twifs_node* twi_node = __twifs_new_node(parent, name, name_len); twi_node->itype = VFS_INODE_TYPE_DIR; - twi_node->fops.readdir = __twifs_iterate_dir; struct v_inode* twi_inode = __twifs_create_inode(twi_node); - struct twifs_node* dot = __twifs_new_node(twi_node, ".", 1); - struct twifs_node* ddot = __twifs_new_node(twi_node, "..", 2); - - dot->itype = VFS_INODE_TYPE_DIR; - ddot->itype = VFS_INODE_TYPE_DIR; - + twi_node->fops.readdir = __twifs_iterate_dir; twi_node->inode = twi_inode; - dot->inode = twi_inode; - ddot->inode = parent ? parent->inode : twi_inode; return twi_node; } @@ -152,13 +138,6 @@ int __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point) { mount_point->inode = fs_root->inode; - // FIXME try to mitigate this special case or pull it up to higher level of - // abstraction - if (mount_point->parent && mount_point->parent->inode) { - struct hstr ddot_name = HSTR("..", 2); - struct twifs_node* root_ddot = __twifs_get_node(fs_root, &ddot_name); - root_ddot->inode = mount_point->parent->inode; - } return 0; } diff --git a/lunaix-os/kernel/fs/vfs.c b/lunaix-os/kernel/fs/vfs.c index 48ec27d..4c03240 100644 --- a/lunaix-os/kernel/fs/vfs.c +++ b/lunaix-os/kernel/fs/vfs.c @@ -39,6 +39,7 @@ static int fs_id = 0; struct hstr vfs_ddot = HSTR("..", 2); struct hstr vfs_dot = HSTR(".", 1); +struct hstr vfs_empty = HSTR("", 0); struct v_dnode* vfs_d_alloc(); @@ -86,9 +87,13 @@ __dcache_get_bucket(struct v_dnode* parent, unsigned int hash) struct v_dnode* vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str) { - if (!str->len) + if (!str->len || HSTR_EQ(str, &vfs_dot)) return parent; + if (HSTR_EQ(str, &vfs_ddot)) { + return parent->parent ? parent->parent : parent; + } + struct hbucket* slot = __dcache_get_bucket(parent, str->hash); struct v_dnode *pos, *n; @@ -288,6 +293,9 @@ vfs_open(struct v_dnode* dnode, struct v_file** file) struct v_file* vfile = cake_grab(file_pile); memset(vfile, 0, sizeof(*vfile)); + vfile->dnode = dnode; + vfile->inode = dnode->inode; + int errno = dnode->inode->ops.open(dnode->inode, vfile); if (errno) { cake_release(file_pile, vfile); @@ -357,6 +365,7 @@ vfs_d_alloc() struct v_dnode* dnode = cake_grab(dnode_pile); memset(dnode, 0, sizeof(*dnode)); llist_init_head(&dnode->children); + dnode->name = vfs_empty; return dnode; } @@ -417,8 +426,6 @@ __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options) struct v_file* opened_file; int errno = __vfs_do_open(&opened_file, path, options), fd; - __current->k_status = errno; - if (!errno && !(errno = vfs_alloc_fdslot(&fd))) { struct v_fd* fd_s = vzalloc(sizeof(*fd_s)); fd_s->file = opened_file; @@ -427,6 +434,7 @@ __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options) return fd; } + __current->k_status = errno; return SYSCALL_ESTATUS(errno); } @@ -475,11 +483,21 @@ __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent) .index = dent->d_offset, .read_complete_callback = __vfs_readdir_callback }; - if (!(errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) { - dent->d_offset++; + if (dent->d_offset == 0) { + __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0); + } else if (dent->d_offset == 1) { + __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0); + } else { + dctx.index -= 2; + if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) { + goto done; + } } + errno = 0; + dent->d_offset++; } +done: __current->k_status = errno; return SYSCALL_ESTATUS(errno); } @@ -576,6 +594,80 @@ __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options) fd_s->pos = fpos; } + __current->k_status = errno; + return SYSCALL_ESTATUS(errno); +} + +int +vfs_readlink(struct v_dnode* dnode, char* buf, size_t size, int depth) +{ + if (!dnode) { + return 0; + } + + if (depth > 64) { + return ELOOP; + } + + size_t len = vfs_readlink(dnode->parent, buf, size, depth + 1); + + if (len >= size) { + return len; + } + + size_t cpy_size = MIN(dnode->name.len, size - len); + strncpy(buf + len, dnode->name.value, cpy_size); + len += cpy_size; + + if (len < size) { + buf[len++] = PATH_DELIM; + } + + return len; +} + +__DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size) +{ + int errno; + struct v_dnode* dnode; + if (!(errno = vfs_walk(NULL, path, &dnode, NULL, 0))) { + errno = vfs_readlink(dnode, buf, size, 0); + } + + if (errno >= 0) { + return errno; + } + + __current->k_status = errno; + return SYSCALL_ESTATUS(errno); +} + +__DEFINE_LXSYSCALL4(int, + readlinkat, + int, + dirfd, + const char*, + pathname, + char*, + buf, + size_t, + size) +{ + int errno; + struct v_fd* fd_s; + if (!GET_FD(dirfd, fd_s)) { + errno = EBADF; + } else { + struct v_dnode* dnode; + if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) { + errno = vfs_readlink(fd_s->file->dnode, buf, size, 0); + } + } + + if (errno >= 0) { + return errno; + } + __current->k_status = errno; return SYSCALL_ESTATUS(errno); } \ No newline at end of file diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index ec99742..3aa402e 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -43,8 +43,8 @@ __do_reserved_memory(int unlock); #define USE_DEMO // #define DEMO_SIGNAL -// #define DEMO_READDIR -#define DEMO_IOTEST +#define DEMO_READDIR +//#define DEMO_IOTEST extern void _pconsole_main(); -- 2.27.0