vfs.hpp 2.5 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. public:
  14. using filldir_func = std::function<int(const char*, size_t, inode*, uint8_t)>;
  15. private:
  16. // TODO: use allocator designed for small objects
  17. using inode_list = std::map<ino_t, inode>;
  18. private:
  19. inode_list _inodes;
  20. types::hash_map<dentry*, dentry*> _mount_recover_list;
  21. protected:
  22. dentry _root;
  23. protected:
  24. inode* cache_inode(size_t size, ino_t ino, mode_t mode, uid_t uid, gid_t gid);
  25. void free_inode(ino_t ino);
  26. inode* get_inode(ino_t ino);
  27. void register_root_node(inode* root);
  28. int load_dentry(dentry* ent);
  29. public:
  30. vfs();
  31. vfs(const vfs&) = delete;
  32. vfs& operator=(const vfs&) = delete;
  33. vfs(vfs&&) = delete;
  34. vfs& operator=(vfs&&) = delete;
  35. constexpr dentry* root(void)
  36. {
  37. return &_root;
  38. }
  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 operation
  48. virtual int inode_statx(dentry* dent, statx* buf, unsigned int mask);
  49. virtual int inode_stat(dentry* dent, struct stat* stat);
  50. // file operations
  51. virtual size_t read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  52. virtual size_t write(inode* file, const char* buf, size_t offset, size_t n);
  53. virtual int dev_id(inode* file, dev_t& out_dev);
  54. virtual int readlink(inode* file, char* buf, size_t buf_size);
  55. virtual int truncate(inode* file, size_t size);
  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 int readdir(inode* dir, size_t offset, const filldir_func& callback) = 0;
  65. };
  66. } // namespace fs