4 * @brief RamFS - a file system sit in RAM
8 * @copyright Copyright (c) 2022
11 #include <lunaix/fs.h>
12 #include <lunaix/fs/ramfs.h>
13 #include <lunaix/mm/valloc.h>
16 A RAM FS will play a role of being a root.
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.
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.
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.
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.
42 volatile static inode_t ino = 0;
44 extern const struct v_inode_ops ramfs_inode_ops;
45 extern const struct v_file_ops ramfs_file_ops;
48 ramfs_readdir(struct v_file* file, struct dir_context* dctx)
51 struct v_dnode *pos, *n;
52 llist_for_each(pos, n, &file->dnode->children, siblings)
54 if (i++ >= dctx->index) {
55 dctx->read_complete_callback(dctx,
58 vfs_get_dtype(pos->inode->itype));
66 ramfs_mkdir(struct v_inode* this, struct v_dnode* dnode)
68 struct v_inode* inode = vfs_i_alloc(dnode->super_block);
69 inode->itype = VFS_IFDIR;
72 vfs_assign_inode(dnode, inode);
78 ramfs_create(struct v_inode* this, struct v_dnode* dnode)
80 struct v_inode* inode = vfs_i_alloc(dnode->super_block);
81 inode->itype = VFS_IFFILE;
84 vfs_assign_inode(dnode, inode);
90 ramfs_inode_init(struct v_superblock* vsb, struct v_inode* inode)
93 inode->ops = &ramfs_inode_ops;
94 inode->default_fops = &ramfs_file_ops;
98 ramfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
100 vsb->ops.init_inode = ramfs_inode_init;
102 struct v_inode* inode = vfs_i_alloc(vsb);
103 inode->itype = VFS_IFDIR;
105 vfs_i_addhash(inode);
106 vfs_assign_inode(mount_point, inode);
112 ramfs_unmount(struct v_superblock* vsb)
120 struct filesystem* ramfs = fsm_new_fs("ramfs", -1);
121 ramfs->mount = ramfs_mount;
122 ramfs->unmount = ramfs_unmount;
127 const struct v_inode_ops ramfs_inode_ops = { .mkdir = ramfs_mkdir,
128 .rmdir = default_inode_rmdir,
130 default_inode_dirlookup,
131 .create = ramfs_create,
132 .open = default_inode_open,
133 .rename = default_inode_rename };
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 };