blob: 7b4ff0b218a5962612d793154d494d8bc04cf401 [file] [log] [blame]
swissChili8efa4922021-03-02 16:34:49 -08001#include "vfs.h"
swissChilif5448622021-03-08 20:17:36 -08002#include "io.h"
swissChili8efa4922021-03-02 16:34:49 -08003
swissChili6c0519e2021-03-07 19:40:23 -08004struct fs_node root, dev, initrd;
swissChilif5448622021-03-08 20:17:36 -08005struct fs_vtable root_v, dev_v;
swissChili6c0519e2021-03-07 19:40:23 -08006
swissChili8efa4922021-03-02 16:34:49 -08007uint fs_read(struct fs_node *node, size_t offset, size_t size, uchar *buffer)
8{
9 if (!node || !node->vtable || !node->vtable->read)
10 return 0;
11
12 return node->vtable->read(node, offset, size, buffer);
13}
14
15uint fs_write(struct fs_node *node, size_t offset, size_t size, uchar *buffer)
16{
17 if (!node || !node->vtable || !node->vtable->write)
18 return 0;
19
20 return node->vtable->write(node, offset, size, buffer);
21}
22
23void fs_open(struct fs_node *node)
24{
25 if (!node || !node->vtable || !node->vtable->open)
26 return;
27
28 node->vtable->open(node);
29}
30
31void fs_close(struct fs_node *node)
32{
33 if (!node || !node->vtable || !node->vtable->close)
34 return;
35
36 node->vtable->close(node);
37}
38
swissChilif5448622021-03-08 20:17:36 -080039uint fs_readdir(struct fs_node *node, uint index, struct fs_dirent *out)
swissChili8efa4922021-03-02 16:34:49 -080040{
swissChilif5448622021-03-08 20:17:36 -080041 if (!node || !node->vtable || !node->vtable->readdir ||
42 (node->flags & 7) != FS_DIRECTORY)
43 return 1;
swissChili8efa4922021-03-02 16:34:49 -080044
swissChilif5448622021-03-08 20:17:36 -080045 return node->vtable->readdir(node, index, out);
swissChili8efa4922021-03-02 16:34:49 -080046}
47
swissChilif5448622021-03-08 20:17:36 -080048uint fs_finddir(struct fs_node *node, char *name, struct fs_node *out)
swissChili8efa4922021-03-02 16:34:49 -080049{
swissChilif5448622021-03-08 20:17:36 -080050 if (!node || !node->vtable || !node->vtable->finddir ||
51 (node->flags & 7) != FS_DIRECTORY)
52 return 1;
swissChili8efa4922021-03-02 16:34:49 -080053
swissChilif5448622021-03-08 20:17:36 -080054 return node->vtable->finddir(node, name, out);
swissChili8efa4922021-03-02 16:34:49 -080055}
swissChili6c0519e2021-03-07 19:40:23 -080056
swissChilif5448622021-03-08 20:17:36 -080057uint root_readdir(struct fs_node *node, uint index, struct fs_dirent *out)
swissChili6c0519e2021-03-07 19:40:23 -080058{
swissChilif5448622021-03-08 20:17:36 -080059 if (node == &root && index == 0)
60 {
61 // devfs
62 memcpy(out->name, "dev", 4);
63 out->inode = -1;
swissChili6c0519e2021-03-07 19:40:23 -080064
swissChilif5448622021-03-08 20:17:36 -080065 return 0;
66 }
67 else
68 return 1;
69}
70
71uint root_finddir(struct fs_node *node, char *name, struct fs_node *out)
72{
73 if (!strcmp(name, "dev"))
74 {
75 *out = dev;
76 return 0;
77 }
78
79 return 1;
80}
81
82uint dev_readdir(struct fs_node *node, uint index, struct fs_dirent *out)
83{
84 if (node == &dev && index == 0)
85 {
86 // initrd
87 memcpy(out->name, "dirent", 7);
88 out->inode = -1;
89 return 0;
90 }
91 else
92 return 1;
93}
94
95uint dev_finddir(struct fs_node *node, char *name, struct fs_node *out)
96{
97 if (node != &dev)
98 return 1;
99
100 if (!strcmp(name, "initrd"))
101 {
102 *out = initrd;
103 return 0;
104 }
105
106 return 1;
swissChili6c0519e2021-03-07 19:40:23 -0800107}
108
109void init_vfs()
110{
swissChilif5448622021-03-08 20:17:36 -0800111 memset(&root_v, 0, sizeof(root_v));
112 memset(&dev_v, 0, sizeof(root_v));
113 memset(&root, 0, sizeof(root));
114 memset(&dev, 0, sizeof(dev));
115
116 root.flags = FS_DIRECTORY;
117 root.vtable = &root_v;
118 dev.flags = FS_DIRECTORY;
119 dev.vtable = &dev_v;
120 initrd.flags = FS_DIRECTORY;
121
122 root_v.readdir = root_readdir;
123 root_v.finddir = root_finddir;
124}
125
126uint fs_mount(struct fs_node *target, struct fs_node *source)
127{
128 if (target->flags ^ FS_DIRECTORY)
129 return 1; // target is not a directory
130
131 if (target->flags & FS_MOUNT)
132 return 2; // already mounted
133
134 if (source->flags ^ FS_DIRECTORY)
135 return 3; // source is not a directory
136
137 target->flags |= FS_MOUNT;
138 target->mount = source;
139
140 return 0;
swissChili6c0519e2021-03-07 19:40:23 -0800141}