feat: implement readlink(2) readlinkat(2)
authorMinep <zelong56@gmail.com>
Sat, 30 Jul 2022 16:12:54 +0000 (17:12 +0100)
committerMinep <zelong56@gmail.com>
Sat, 30 Jul 2022 16:12:54 +0000 (17:12 +0100)
refactor: pull the . and .. support to vfs level
chore: refactor and bug fixes

README.md
lunaix-os/includes/lunaix/fctrl.h
lunaix-os/includes/lunaix/fs.h
lunaix-os/includes/lunaix/status.h
lunaix-os/includes/lunaix/syscall.h
lunaix-os/kernel/asm/x86/syscall.S
lunaix-os/kernel/demos/dir_read.c
lunaix-os/kernel/fs/twifs.c
lunaix-os/kernel/fs/vfs.c
lunaix-os/kernel/proc0.c

index 8e35534d8d0c4742511073a73432c0e784f797dc..b995657869e059f8bb0a1262cd687917c3987ce9 100644 (file)
--- 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自有
 
index e76ae7b05263c405624cf113ba7aabacc67bacd2..0cd2705e4d9c1ce984f66c91ff7ff118051f6794 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <lunaix/dirent.h>
 #include <lunaix/syscall.h>
+#include <stddef.h>
 
 __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 */
index fe9b39b6ea2cdff27de62b585c18a4bccf82a38c..7f05413cdb08310ddd66e41ba4afb7adf3798509 100644 (file)
@@ -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绑定他的一些专有数据
index 2c3ff963ddc4eff7311a530674b9c13dcc12e4ac..17b509e08e0ecfefe7c5555f4a8c80f85200819d 100644 (file)
@@ -18,5 +18,6 @@
 #define EBADF -13
 #define ENOTSUP -14
 #define ENXIO -15
+#define ELOOP -16
 
 #endif /* __LUNAIX_CODE_H */
index 88aa0261840492ad503180c25a03de99b0a439da..f5b72d36573febb15b5ee85ddf28701f34bd69f5 100644 (file)
@@ -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)                                  \
index 6a8b43277135c5155c29f4c3dea37fe11c7dbbd8..61b9879609f7d89397d4bcdefd11952546e56956 100644 (file)
@@ -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
index 7a63ba933d7c44469eedaee1de02b3655f66c2c3..a0ef65f41ab542b777ad3fbf3842d67603af6e2f 100644 (file)
@@ -1,5 +1,6 @@
 #include <lunaix/dirent.h>
 #include <lunaix/fctrl.h>
+#include <lunaix/proc.h>
 #include <lunaix/syslog.h>
 
 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;
index 37af44e2777fcf9bf7c3fb5139ba749b26aa192f..104a2fba8e9edceb4e0ce0f68d582210f7ffd01e 100644 (file)
@@ -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;
 }
 
index 48ec27dc18d06155be990d6926f20b1da413f993..4c0324088d0a60eaddd486e90a7cc7676cbd5cfc 100644 (file)
@@ -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
index ec997425b9c0161ab817d19f91591c9c4c5585d2..3aa402ed42d16befb2ccb9dec032d7832244fdc4 100644 (file)
@@ -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();