4 * @brief RamFS - a file system sit in RAM
8 * @copyright Copyright (c) 2022
11 #include <klibc/string.h>
12 #include <lunaix/fs.h>
13 #include <lunaix/fs/ramfs.h>
14 #include <lunaix/mm/valloc.h>
15 #include <lunaix/spike.h>
18 A RAM FS will play a role of being a root.
20 This is an temporary meaure as we don't have any concrete fs
21 yet. In the near future, we will have an ISO-9660 as our first
22 fs and mount our boot medium as root. And similar thing could
23 be done when we have installed our OS into hard disk, in that
24 case, our rootfs will be something like ext2.
26 RamFS vs. TwiFS: Indeed, they are both fs that lives in RAM so
27 there is no foundmental differences. However, TwiFS is designed
28 to be a 'virtual FIlesystem for KERnel space' (FIKER), so other
29 kernel sub-modules can just create node and attach their own
30 implementation of read/write, without brothering to create a
31 full featured concrete filesystem. This particularly useful for
32 kernel state exposure. In Lunaix, TwiFS is summation of procfs,
33 sysfs and kernfs in Linux world.
35 However, there is a smell of bad-design in the concept of TwiFS.
36 Since it tries to integrate too much responsibility. The TwiFS may
37 be replaced by more specific fs in the future.
39 On the other hand, RamFS is designed to be act like a dummy fs
40 that just ensure 'something there at least' and thus provide basic
41 'mountibility' for other fs.
44 static volatile inode_t ino = 0;
46 extern const struct v_inode_ops ramfs_inode_ops;
47 extern const struct v_file_ops ramfs_file_ops;
50 __ramfs_mknod(struct v_dnode* dnode, struct v_inode** nod_out, u32_t flags)
52 struct v_inode* inode = vfs_i_alloc(dnode->super_block);
58 struct ram_inode* rinode = valloc(sizeof(struct ram_inode));
65 rinode->flags = flags;
68 if ((flags & RAMF_DIR)) {
69 inode->itype = VFS_IFDIR;
71 inode->itype = VFS_IFFILE;
74 if ((flags & RAMF_SYMLINK)) {
75 inode->itype |= VFS_IFSYMLINK;
83 vfs_assign_inode(dnode, inode);
89 ramfs_readdir(struct v_file* file, struct dir_context* dctx)
92 struct v_dnode *pos, *n;
93 llist_for_each(pos, n, &file->dnode->children, siblings)
95 if (i++ >= dctx->index) {
96 dctx->read_complete_callback(dctx,
99 vfs_get_dtype(pos->inode->itype));
107 ramfs_mkdir(struct v_inode* this, struct v_dnode* dnode)
109 return __ramfs_mknod(dnode, NULL, RAMF_DIR);
113 ramfs_create(struct v_inode* this, struct v_dnode* dnode)
115 return __ramfs_mknod(dnode, NULL, RAMF_FILE);
119 ramfs_inode_init(struct v_superblock* vsb, struct v_inode* inode)
122 inode->ops = (struct v_inode_ops*)&ramfs_inode_ops;
123 inode->default_fops = (struct v_file_ops*)&ramfs_file_ops;
127 ramfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
129 vsb->ops.init_inode = ramfs_inode_init;
131 return __ramfs_mknod(mount_point, NULL, RAMF_DIR);
135 ramfs_unmount(struct v_superblock* vsb)
143 struct filesystem* ramfs = fsm_new_fs("ramfs", -1);
144 ramfs->mount = ramfs_mount;
145 ramfs->unmount = ramfs_unmount;
149 EXPORT_FILE_SYSTEM(ramfs, ramfs_init);
152 ramfs_mksymlink(struct v_inode* this, const char* target)
154 struct ram_inode* rinode = RAM_INODE(this->data);
156 assert(!(rinode->flags & RAMF_SYMLINK));
158 size_t len = strlen(target);
159 char* symlink = valloc(len);
165 memcpy(symlink, target, len);
167 this->itype |= VFS_IFSYMLINK;
168 rinode->flags |= RAMF_SYMLINK;
169 rinode->symlink = symlink;
175 ramfs_read_symlink(struct v_inode* this, const char** path_out)
177 struct ram_inode* rinode = RAM_INODE(this->data);
179 if (!(rinode->flags & RAMF_SYMLINK)) {
183 *path_out = rinode->symlink;
189 ramfs_unlink(struct v_inode* this)
191 struct ram_inode* rinode = RAM_INODE(this->data);
193 if ((rinode->flags & RAMF_SYMLINK)) {
194 rinode->flags &= ~RAMF_SYMLINK;
195 this->itype &= ~VFS_IFSYMLINK;
197 vfree(rinode->symlink);
207 const struct v_inode_ops ramfs_inode_ops = { .mkdir = ramfs_mkdir,
208 .rmdir = default_inode_rmdir,
210 default_inode_dirlookup,
211 .create = ramfs_create,
212 .open = default_inode_open,
213 .unlink = ramfs_unlink,
214 .set_symlink = ramfs_mksymlink,
215 .read_symlink = ramfs_read_symlink,
216 .rename = default_inode_rename };
218 const struct v_file_ops ramfs_file_ops = { .readdir = ramfs_readdir,
219 .close = default_file_close,
220 .read = default_file_read,
221 .read_page = default_file_read,
222 .write = default_file_write,
223 .write_page = default_file_write,
224 .seek = default_file_seek };