vfs.hpp 2.4 KB

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