vfs.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <defs.hpp>
  3. #include <functional>
  4. #include <map>
  5. #include <stdint.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <kernel/vfs/dentry.hpp>
  9. #include <kernel/vfs/inode.hpp>
  10. namespace fs {
  11. class vfs {
  12. public:
  13. using filldir_func = std::function<ssize_t(const char*, inode*, u8)>;
  14. private:
  15. struct dcache m_dcache;
  16. struct dentry* m_root{};
  17. std::map<ino_t, inode> m_inodes;
  18. protected:
  19. dev_t m_device;
  20. size_t m_io_blksize;
  21. protected:
  22. vfs(dev_t device, size_t io_blksize);
  23. inode* alloc_inode(ino_t ino);
  24. void free_inode(ino_t ino);
  25. inode* get_inode(ino_t ino);
  26. void register_root_node(inode* root);
  27. public:
  28. static std::pair<vfs*, int> create(const char* source, const char* fstype,
  29. unsigned long flags, const void* data);
  30. vfs(const vfs&) = delete;
  31. vfs& operator=(const vfs&) = delete;
  32. vfs(vfs&&) = delete;
  33. vfs& operator=(vfs&&) = delete;
  34. struct dentry* root() const noexcept;
  35. dev_t fs_device() const noexcept;
  36. size_t io_blksize() const noexcept;
  37. int mount(dentry* mnt, const char* source, const char* mount_point,
  38. const char* fstype, unsigned long flags, const void* data);
  39. // directory operations
  40. virtual int creat(struct inode* dir, dentry* at, mode_t mode);
  41. virtual int mkdir(struct inode* dir, dentry* at, mode_t mode);
  42. virtual int mknod(struct inode* dir, dentry* at, mode_t mode, dev_t device);
  43. virtual int unlink(struct inode* dir, dentry* at);
  44. virtual int symlink(struct inode* dir, dentry* at, const char* target);
  45. // metadata operations
  46. int statx(inode* ind, struct statx* st, unsigned int mask);
  47. // file operations
  48. virtual ssize_t read(inode* file, char* buf, size_t buf_size, size_t count,
  49. off_t offset);
  50. virtual ssize_t write(inode* file, const char* buf, size_t count,
  51. off_t offset);
  52. virtual dev_t i_device(inode* ind);
  53. virtual int readlink(inode* file, char* buf, size_t buf_size);
  54. virtual int truncate(inode* file, size_t size);
  55. // directory operations
  56. // parameter 'length' in callback:
  57. // if 0, 'name' should be null terminated
  58. // else, 'name' size
  59. //
  60. // @return
  61. // return -1 if an error occurred
  62. // return 0 if no more entry available
  63. // otherwise, return bytes to be added to the offset
  64. virtual ssize_t readdir(inode* dir, size_t offset,
  65. const filldir_func& callback) = 0;
  66. };
  67. } // namespace fs