vfs.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include <bits/alltypes.h>
  3. #include <stdint.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <types/path.hpp>
  7. #include <kernel/mem/paging.hpp>
  8. #include <kernel/vfs/dentry.hpp>
  9. #include <kernel/vfs/file.hpp>
  10. #define NODE_MAJOR(node) (((node) >> 8) & 0xFFU)
  11. #define NODE_MINOR(node) ((node) & 0xFFU)
  12. namespace fs {
  13. constexpr dev_t make_device(uint32_t major, uint32_t minor) {
  14. return ((major << 8) & 0xFF00U) | (minor & 0xFFU);
  15. }
  16. // buf, buf_size, offset, cnt
  17. using blkdev_read =
  18. std::function<ssize_t(char*, std::size_t, std::size_t, std::size_t)>;
  19. // buf, offset, cnt
  20. using blkdev_write =
  21. std::function<ssize_t(const char*, std::size_t, std::size_t)>;
  22. struct blkdev_ops {
  23. blkdev_read read;
  24. blkdev_write write;
  25. };
  26. // buf, buf_size, cnt
  27. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  28. // buf, cnt
  29. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  30. struct chrdev_ops {
  31. chrdev_read read;
  32. chrdev_write write;
  33. };
  34. struct PACKED user_dirent {
  35. ino_t d_ino; // inode number
  36. uint32_t d_off; // ignored
  37. uint16_t d_reclen; // length of this struct user_dirent
  38. char d_name[1]; // file name with a padding zero
  39. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  40. };
  41. struct PACKED user_dirent64 {
  42. ino64_t d_ino; // inode number
  43. uint64_t d_off; // implementation-defined field, ignored
  44. uint16_t d_reclen; // length of this struct user_dirent
  45. uint8_t d_type; // file type, with offset of (d_reclen - 1)
  46. char d_name[1]; // file name with a padding zero
  47. };
  48. struct fs_context {
  49. dentry_pointer root;
  50. };
  51. int register_block_device(dev_t node, const blkdev_ops& ops);
  52. int register_char_device(dev_t node, const chrdev_ops& ops);
  53. void partprobe();
  54. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset,
  55. size_t n);
  56. ssize_t block_device_write(dev_t node, const char* buf, size_t offset,
  57. size_t n);
  58. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  59. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  60. extern "C" int fs_creat(struct dentry* at, mode_t mode);
  61. extern "C" int fs_mkdir(struct dentry* at, mode_t mode);
  62. extern "C" int fs_mknod(struct dentry* at, mode_t mode, dev_t sn);
  63. extern "C" int fs_unlink(struct dentry* at);
  64. extern "C" int fs_symlink(struct dentry* at, const char* target);
  65. extern "C" int fs_statx(const struct rust_inode_handle* inode,
  66. struct statx* stat, unsigned int mask);
  67. extern "C" int fs_readlink(const struct rust_inode_handle* inode, char* buf,
  68. size_t buf_size);
  69. extern "C" int fs_truncate(const struct rust_inode_handle* file, size_t size);
  70. extern "C" size_t fs_read(const struct rust_inode_handle* file, char* buf,
  71. size_t buf_size, size_t offset, size_t n);
  72. extern "C" size_t fs_write(const struct rust_inode_handle* file,
  73. const char* buf, size_t offset, size_t n);
  74. using readdir_callback_fn =
  75. std::function<int(const char*, size_t, const struct rust_inode_handle*,
  76. const struct inode_data*, uint8_t)>;
  77. extern "C" ssize_t fs_readdir(const struct rust_inode_handle* file,
  78. size_t offset,
  79. const readdir_callback_fn* callback);
  80. extern "C" int fs_mount(dentry* mnt, const char* source,
  81. const char* mount_point, const char* fstype,
  82. unsigned long flags, const void* data);
  83. extern "C" struct dentry* r_get_mountpoint(struct dentry* mnt);
  84. extern "C" mode_t r_dentry_save_inode(struct dentry* dent,
  85. const struct rust_inode_handle* inode);
  86. extern "C" mode_t r_get_inode_mode(const struct rust_inode_handle* inode);
  87. extern "C" size_t r_get_inode_size(const struct rust_inode_handle* inode);
  88. extern "C" struct dentry* r_get_root_dentry();
  89. #define current_open(...) \
  90. fs::open(current_process->fs_context, current_process->cwd.get(), \
  91. __VA_ARGS__)
  92. std::pair<dentry_pointer, int> open(const fs_context& context, dentry* cwd,
  93. types::path_iterator path,
  94. bool follow_symlinks = true,
  95. int recurs_no = 0);
  96. } // namespace fs