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