vfs.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #define NODE_MAJOR(node) (((node) >> 8) & 0xFFU)
  10. #define NODE_MINOR(node) ((node) & 0xFFU)
  11. namespace fs {
  12. constexpr dev_t make_device(uint32_t major, uint32_t minor) {
  13. return ((major << 8) & 0xFF00U) | (minor & 0xFFU);
  14. }
  15. // buf, buf_size, cnt
  16. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  17. // buf, cnt
  18. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  19. struct chrdev_ops {
  20. chrdev_read read;
  21. chrdev_write write;
  22. };
  23. int register_char_device(dev_t node, const chrdev_ops& ops);
  24. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  25. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  26. class rust_file_array {
  27. public:
  28. struct handle;
  29. private:
  30. struct handle* m_handle;
  31. public:
  32. rust_file_array(struct handle* handle);
  33. rust_file_array(const rust_file_array&) = delete;
  34. ~rust_file_array();
  35. constexpr rust_file_array(rust_file_array&& other) noexcept
  36. : m_handle(std::exchange(other.m_handle, nullptr)) {}
  37. struct handle* get() const;
  38. void drop();
  39. };
  40. class rust_fs_context {
  41. public:
  42. struct handle;
  43. private:
  44. struct handle* m_handle;
  45. public:
  46. rust_fs_context(struct handle* handle);
  47. rust_fs_context(const rust_fs_context&) = delete;
  48. ~rust_fs_context();
  49. constexpr rust_fs_context(rust_fs_context&& other) noexcept
  50. : m_handle(std::exchange(other.m_handle, nullptr)) {}
  51. struct handle* get() const;
  52. void drop();
  53. };
  54. extern "C" size_t fs_read(struct dentry* file, char* buf, size_t buf_size, size_t offset, size_t n);
  55. } // namespace fs