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 | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 39 | uint 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) |
| 43 | return 1; |
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 | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 48 | uint fs_finddir(struct fs_node *node, char *name, struct fs_node *out) |
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) |
| 52 | return 1; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 53 | |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 54 | return node->vtable->finddir(node, name, out); |
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 | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 57 | uint 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 | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 65 | return 0; |
| 66 | } |
| 67 | else |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | uint 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 | |
| 82 | uint 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 | |
| 95 | uint 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; |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void init_vfs() |
| 110 | { |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame^] | 111 | 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 | |
| 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 | } |