swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 1 | #include "vfs.h" |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 2 | #include "io.h" |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 3 | |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 4 | struct fs_node root, dev, initrd; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 5 | struct fs_vtable root_v, dev_v; |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 6 | |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 7 | uint 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 | |
| 15 | uint 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 | |
| 23 | void 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 | |
| 31 | void 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 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 39 | bool fs_readdir(struct fs_node *node, uint index, struct fs_dirent *out) |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 40 | { |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 41 | if (!node || !node->vtable || !node->vtable->readdir || |
| 42 | (node->flags & 7) != FS_DIRECTORY) |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 43 | return false; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 44 | |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 45 | return node->vtable->readdir(node, index, out); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 46 | } |
| 47 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 48 | struct fs_node *fs_finddir(struct fs_node *node, char *name) |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 49 | { |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 50 | if (!node || !node->vtable || !node->vtable->finddir || |
| 51 | (node->flags & 7) != FS_DIRECTORY) |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 52 | return NULL; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 53 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 54 | return node->vtable->finddir(node, name); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 55 | } |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 56 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 57 | bool root_readdir(struct fs_node *node, uint index, struct fs_dirent *out) |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 58 | { |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 59 | if (node == &root && index == 0) |
| 60 | { |
| 61 | // devfs |
| 62 | memcpy(out->name, "dev", 4); |
| 63 | out->inode = -1; |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 64 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 65 | return true; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 66 | } |
| 67 | else |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 68 | return false; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 69 | } |
| 70 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 71 | struct fs_node *root_finddir(struct fs_node *node, char *name) |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 72 | { |
| 73 | if (!strcmp(name, "dev")) |
| 74 | { |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 75 | return &dev; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 76 | } |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 77 | else return NULL; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 78 | } |
| 79 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 80 | bool dev_readdir(struct fs_node *node, uint index, struct fs_dirent *out) |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 81 | { |
| 82 | if (node == &dev && index == 0) |
| 83 | { |
| 84 | // initrd |
| 85 | memcpy(out->name, "dirent", 7); |
| 86 | out->inode = -1; |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 87 | return true; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 88 | } |
| 89 | else |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 90 | return false; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 91 | } |
| 92 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 93 | struct fs_node *dev_finddir(struct fs_node *node, char *name) |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 94 | { |
| 95 | if (node != &dev) |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 96 | return NULL; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 97 | |
| 98 | if (!strcmp(name, "initrd")) |
| 99 | { |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 100 | return &initrd; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 101 | } |
| 102 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 103 | return NULL; |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void init_vfs() |
| 107 | { |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 108 | memset(&root_v, 0, sizeof(root_v)); |
| 109 | memset(&dev_v, 0, sizeof(root_v)); |
| 110 | memset(&root, 0, sizeof(root)); |
| 111 | memset(&dev, 0, sizeof(dev)); |
| 112 | |
| 113 | root.flags = FS_DIRECTORY; |
| 114 | root.vtable = &root_v; |
| 115 | dev.flags = FS_DIRECTORY; |
| 116 | dev.vtable = &dev_v; |
| 117 | initrd.flags = FS_DIRECTORY; |
| 118 | |
| 119 | root_v.readdir = root_readdir; |
| 120 | root_v.finddir = root_finddir; |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 121 | |
| 122 | dev_v.readdir = dev_readdir; |
| 123 | dev_v.finddir = dev_finddir; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uint 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; |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 141 | } |