swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "kint.h" |
| 4 | |
| 5 | struct fs_vtable; |
| 6 | |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 7 | #define FS_MAX_NAME_LEN 128 |
| 8 | |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 9 | struct fs_node |
| 10 | { |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 11 | /** length of file name */ |
| 12 | uint name_len; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 13 | /** file name */ |
| 14 | char name[128]; |
| 15 | /** identifier */ |
| 16 | uint inode; |
| 17 | /** type of node */ |
| 18 | uint flags; |
| 19 | /** permissions */ |
| 20 | uint mask; |
| 21 | /** group id */ |
| 22 | uint gid; |
| 23 | /** user id */ |
| 24 | uint uid; |
| 25 | /** size in bytes */ |
| 26 | size_t size; |
| 27 | /** reserved for driver */ |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 28 | union |
| 29 | { |
| 30 | uint dri_res_i; |
| 31 | void *dri_res_p; |
| 32 | }; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 33 | |
| 34 | struct fs_vtable *vtable; |
| 35 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 36 | /** used for mounts */ |
| 37 | struct fs_node *mount; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct fs_dirent |
| 41 | { |
swissChili | caa2478 | 2021-07-19 14:29:58 -0700 | [diff] [blame] | 42 | // EXT2 supports up to 256 byte file names, so we will do the same |
| 43 | char name[256]; |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 44 | uint name_len; |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 45 | uint inode; |
| 46 | }; |
| 47 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 48 | typedef uint (*fs_read_t)(struct fs_node *node, size_t offset, size_t size, uchar *buffer); |
| 49 | typedef uint (*fs_write_t)(struct fs_node *node, size_t offset, size_t size, uchar *buffer); |
| 50 | typedef void (*fs_open_t)(struct fs_node *node); |
| 51 | typedef void (*fs_close_t)(struct fs_node *node); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 52 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 53 | typedef bool (*fs_readdir_t)(struct fs_node *node, uint index, struct fs_dirent *dirent); |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 54 | typedef struct fs_node *(*fs_finddir_t)(struct fs_node *node, char *name, uint name_len); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 55 | |
| 56 | struct fs_vtable |
| 57 | { |
| 58 | fs_read_t read; |
| 59 | fs_write_t write; |
| 60 | fs_open_t open; |
| 61 | fs_close_t close; |
| 62 | fs_readdir_t readdir; |
| 63 | fs_finddir_t finddir; |
| 64 | }; |
| 65 | |
| 66 | enum fs_flags |
| 67 | { |
| 68 | FS_FILE = 1, |
| 69 | FS_DIRECTORY, |
| 70 | FS_CHARDEVICE, |
| 71 | FS_BLOCKDEVICE, |
| 72 | FS_PIPE, |
| 73 | FS_SYMLINK, |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 74 | |
| 75 | FS_MOUNT = 8, /* Can be or'd with others */ |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 78 | extern struct fs_node root, dev, initrd; |
| 79 | |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 80 | /* Not to be confused normal open, close, etc functions, these operate |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 81 | * on the VFS directly |
| 82 | * read and write return the number of bytes written/read, */ |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 83 | |
| 84 | uint fs_read(struct fs_node *node, size_t offset, size_t size, uchar *buffer); |
| 85 | uint fs_write(struct fs_node *node, size_t offset, size_t size, uchar *buffer); |
| 86 | void fs_open(struct fs_node *node); |
| 87 | void fs_close(struct fs_node *node); |
| 88 | |
swissChili | b7fe899 | 2021-03-10 16:25:47 -0800 | [diff] [blame] | 89 | bool fs_readdir(struct fs_node *node, uint index, struct fs_dirent *out); |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 90 | struct fs_node *fs_finddir(struct fs_node *node, char *name, uint name_len); |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 91 | |
| 92 | /* Returns the following error codes: |
| 93 | * 0: success |
| 94 | * 1: target is not a directory |
| 95 | * 2: target is already a mount point |
| 96 | * 3: source is not a directory */ |
| 97 | uint fs_mount(struct fs_node *target, struct fs_node *source); |
swissChili | 6c0519e | 2021-03-07 19:40:23 -0800 | [diff] [blame] | 98 | |
| 99 | void init_vfs(); |