vfs.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <kernel/vfs/inode.hpp>
  3. #include <kernel/vfs/dentry.hpp>
  4. #include <functional>
  5. #include <map>
  6. #include <stdint.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <types/hash_map.hpp>
  10. namespace fs {
  11. class vfs {
  12. public:
  13. using filldir_func = std::function<ssize_t(const char*, size_t, inode*, uint8_t)>;
  14. private:
  15. std::map<ino_t, inode> m_inodes;
  16. types::hash_map<dentry*, dentry*> m_mount_recover_list;
  17. protected:
  18. dentry m_root;
  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. int load_dentry(dentry* ent);
  28. public:
  29. vfs(const vfs&) = delete;
  30. vfs& operator=(const vfs&) = delete;
  31. vfs(vfs&&) = delete;
  32. vfs& operator=(vfs&&) = delete;
  33. constexpr dentry* root(void)
  34. {
  35. return &m_root;
  36. }
  37. dev_t fs_device() const noexcept;
  38. size_t io_blksize() const noexcept;
  39. int mount(dentry* mnt, const char* source, const char* mount_point,
  40. const char* fstype, unsigned long flags, const void* data);
  41. // directory operations
  42. virtual int inode_mkfile(dentry* dir, const char* filename, mode_t mode);
  43. virtual int inode_mknode(dentry* dir, const char* filename, mode_t mode, dev_t sn);
  44. virtual int inode_rmfile(dentry* dir, const char* filename);
  45. virtual int inode_mkdir(dentry* dir, const char* dirname, mode_t mode);
  46. virtual int symlink(dentry* dir, const char* linkname, const char* target);
  47. // metadata operations
  48. int statx(inode* ind, struct statx* st, unsigned int mask);
  49. // file operations
  50. virtual ssize_t read(inode* file, char* buf, size_t buf_size, size_t count, off_t offset);
  51. virtual ssize_t write(inode* file, const char* buf, size_t count, 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, const filldir_func& callback) = 0;
  65. };
  66. } // namespace fs