feat: (iso9660) directory read support
[lunaix-os.git] / lunaix-os / kernel / fs / ramfs / ramfs.c
1 /**
2  * @file ramfs.c
3  * @author Lunaixsky
4  * @brief RamFS - a file system sit in RAM
5  * @version 0.1
6  * @date 2022-08-18
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #include <lunaix/fs.h>
12 #include <lunaix/fs/ramfs.h>
13 #include <lunaix/mm/valloc.h>
14
15 /*
16     A RAM FS will play a role of being a root.
17
18     This is an temporary meaure as we don't have any concrete fs
19     yet. In the near future, we will have an ISO-9660 as our first
20     fs and mount our boot medium as root. And similar thing could
21     be done when we have installed our OS into hard disk, in that
22     case, our rootfs will be something like ext2.
23
24     RamFS vs. TwiFS: Indeed, they are both fs that lives in RAM so
25     there is no foundmentally differences. However, TwiFS is designed
26     to be a 'virtual FIlesystem for KERnel space' (FIKER), so other
27     kernel sub-modules can just create node and attach their own
28     implementation of read/write, without brothering to create a
29     full featured concrete filesystem. This particularly useful for
30     kernel state exposure. In Lunaix, TwiFS is summation of procfs,
31     sysfs and kernfs in Linux world.
32
33     However, there is a smell of bad-design in the concept of TwiFS.
34     Since it tries to integrate too much responsibility. The TwiFS may
35     be replaced by more specific fs in the future.
36
37     On the other hand, RamFS is designed to be act like a dummy fs
38     that just ensure 'something there at least' and thus provide basic
39     'mountibility' for other fs.
40 */
41
42 volatile static inode_t ino = 0;
43
44 extern const struct v_inode_ops ramfs_inode_ops;
45 extern const struct v_file_ops ramfs_file_ops;
46
47 int
48 ramfs_readdir(struct v_file* file, struct dir_context* dctx)
49 {
50     int i = 0;
51     struct v_dnode *pos, *n;
52     llist_for_each(pos, n, &file->dnode->children, siblings)
53     {
54         if (i++ >= dctx->index) {
55             dctx->read_complete_callback(dctx,
56                                          pos->name.value,
57                                          pos->name.len,
58                                          vfs_get_dtype(pos->inode->itype));
59             return 1;
60         }
61     }
62     return 0;
63 }
64
65 int
66 ramfs_mkdir(struct v_inode* this, struct v_dnode* dnode)
67 {
68     struct v_inode* inode = vfs_i_alloc(dnode->super_block);
69     inode->itype = VFS_IFDIR;
70
71     vfs_i_addhash(inode);
72     vfs_assign_inode(dnode, inode);
73
74     return 0;
75 }
76
77 int
78 ramfs_create(struct v_inode* this, struct v_dnode* dnode)
79 {
80     struct v_inode* inode = vfs_i_alloc(dnode->super_block);
81     inode->itype = VFS_IFFILE;
82
83     vfs_i_addhash(inode);
84     vfs_assign_inode(dnode, inode);
85
86     return 0;
87 }
88
89 void
90 ramfs_inode_init(struct v_superblock* vsb, struct v_inode* inode)
91 {
92     inode->id = ino++;
93     inode->ops = &ramfs_inode_ops;
94     inode->default_fops = &ramfs_file_ops;
95 }
96
97 int
98 ramfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
99 {
100     vsb->ops.init_inode = ramfs_inode_init;
101
102     struct v_inode* inode = vfs_i_alloc(vsb);
103     inode->itype = VFS_IFDIR;
104
105     vfs_i_addhash(inode);
106     vfs_assign_inode(mount_point, inode);
107
108     return 0;
109 }
110
111 int
112 ramfs_unmount(struct v_superblock* vsb)
113 {
114     return 0;
115 }
116
117 void
118 ramfs_init()
119 {
120     struct filesystem* ramfs = fsm_new_fs("ramfs", -1);
121     ramfs->mount = ramfs_mount;
122     ramfs->unmount = ramfs_unmount;
123
124     fsm_register(ramfs);
125 }
126
127 const struct v_inode_ops ramfs_inode_ops = { .mkdir = ramfs_mkdir,
128                                              .rmdir = default_inode_rmdir,
129                                              .dir_lookup =
130                                                default_inode_dirlookup,
131                                              .create = ramfs_create,
132                                              .open = default_inode_open,
133                                              .rename = default_inode_rename };
134
135 const struct v_file_ops ramfs_file_ops = { .readdir = ramfs_readdir,
136                                            .close = default_file_close,
137                                            .read = default_file_read,
138                                            .write = default_file_write,
139                                            .seek = default_file_seek };