feat: lseek(2), read(2), write(2) implementation
[lunaix-os.git] / lunaix-os / kernel / fs / twifs.c
index d24b6a0fde64430e7f528d008ec3ada5fd8460ae..ad226ca8d74a2d0069808b7ffadd9a6bd342f11c 100644 (file)
@@ -15,7 +15,7 @@
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/valloc.h>
 
-static struct twifs_node fs_root;
+static struct twifs_node* fs_root;
 
 static struct cake_pile* twi_pile;
 
@@ -28,6 +28,15 @@ __twifs_openfile(struct v_inode* inode, struct v_file* file);
 struct twifs_node*
 __twifs_get_node(struct twifs_node* parent, struct hstr* name);
 
+struct v_inode*
+__twifs_create_inode(struct twifs_node* twi_node);
+
+int
+__twifs_iterate_dir(struct v_file* file, struct dir_context* dctx);
+
+int
+__twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point);
+
 void
 twifs_init()
 {
@@ -39,8 +48,7 @@ twifs_init()
 
     fsm_register(twifs);
 
-    memset(&fs_root, 0, sizeof(fs_root));
-    llist_init_head(&fs_root.children);
+    fs_root = twifs_dir_node(NULL, NULL, 0);
 
     // 预备一些常用的类别
     twifs_toplevel_node("kernel", 6);
@@ -49,36 +57,80 @@ twifs_init()
 }
 
 struct twifs_node*
-twifs_child_node(struct twifs_node* parent, const char* name, int name_len)
+__twifs_new_node(struct twifs_node* parent, const char* name, int name_len)
+{
+    struct twifs_node* node = cake_grab(twi_pile);
+    memset(node, 0, sizeof(*node));
+
+    node->name = HSTR(name, name_len);
+    hstr_rehash(&node->name, HSTR_FULL_HASH);
+    llist_init_head(&node->children);
+
+    if (parent) {
+        llist_append(&parent->children, &node->siblings);
+    }
+
+    return node;
+}
+
+struct twifs_node*
+twifs_file_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_FILE;
+
+    struct v_inode* twi_inode = __twifs_create_inode(twi_node);
+    twi_node->inode = twi_inode;
+
+    return twi_inode;
+}
+
+struct twifs_node*
+twifs_dir_node(struct twifs_node* parent, const char* name, int name_len)
 {
     struct hstr hname = HSTR(name, name_len);
     hstr_rehash(&hname, HSTR_FULL_HASH);
-
     struct twifs_node* node = __twifs_get_node(parent, &hname);
     if (node) {
         return node;
     }
 
-    node = cake_grab(twi_pile);
-    memset(node, 0, sizeof(*node));
+    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;
 
-    node->name = hname;
-    llist_init_head(&node->children);
-    llist_append(&parent->children, &node->siblings);
+    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);
 
-    return node;
+    dot->itype = VFS_INODE_TYPE_DIR;
+    ddot->itype = VFS_INODE_TYPE_DIR;
+
+    twi_node->inode = twi_inode;
+    dot->inode = twi_inode;
+    ddot->inode = parent ? parent->inode : twi_inode;
+
+    return twi_node;
 }
 
 struct twifs_node*
 twifs_toplevel_node(const char* name, int name_len)
 {
-    return twifs_child_node(&fs_root, name, name_len);
+    return twifs_dir_node(fs_root, name, name_len);
 }
 
-void
+int
 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
 {
-    mount_point->inode = __twifs_create_inode(&fs_root);
+    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;
 }
 
 struct v_inode*
@@ -94,11 +146,16 @@ __twifs_create_inode(struct twifs_node* twi_node)
                                .ref_count = 0 };
     inode->ops.dir_lookup = __twifs_dirlookup;
     inode->ops.open = __twifs_openfile;
+
+    return inode;
 }
 
 struct twifs_node*
 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
 {
+    if (!parent)
+        return NULL;
+
     struct twifs_node *pos, *n;
     llist_for_each(pos, n, &parent->children, siblings)
     {
@@ -116,10 +173,10 @@ __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
 
     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
     if (child_node) {
-        dnode->inode = __twifs_create_inode(child_node);
+        dnode->inode = child_node->inode;
         return 0;
     }
-    return VFS_ENODIR;
+    return ENOENT;
 }
 
 int
@@ -133,12 +190,13 @@ __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
     {
         if (counter++ >= dctx->index) {
             dctx->index = counter;
-            dctx->read_complete_callback(dctx, pos->name.value, pos->itype);
+            dctx->read_complete_callback(
+              dctx, pos->name.value, pos->name.len, pos->itype);
             return 0;
         }
     }
 
-    return VFS_EENDOFDIR;
+    return 1;
 }
 
 int
@@ -146,8 +204,9 @@ __twifs_openfile(struct v_inode* inode, struct v_file* file)
 {
     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
     if (twi_node) {
+        file->inode = twi_node->inode;
         file->ops = twi_node->fops;
-        return 1;
+        return 0;
     }
-    return 0;
+    return ENOTSUP;
 }