vfs.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #pragma once
  2. #include <types/allocator.hpp>
  3. #include <types/hash_map.hpp>
  4. #include <types/list.hpp>
  5. #include <types/stdint.h>
  6. #include <types/types.h>
  7. #include <types/vector.hpp>
  8. #define INODE_FILE (1 << 0)
  9. #define INODE_DIR (1 << 1)
  10. #define INODE_MNT (1 << 2)
  11. #define INODE_NODE (1 << 3)
  12. namespace fs {
  13. using ino_t = size_t;
  14. using blksize_t = size_t;
  15. using blkcnt_t = size_t;
  16. class vfs;
  17. union inode_flags {
  18. uint32_t v;
  19. struct {
  20. uint32_t file : 1;
  21. uint32_t directory : 1;
  22. uint32_t mount_point : 1;
  23. uint32_t special_node : 1;
  24. } in;
  25. };
  26. struct inode {
  27. inode_flags flags;
  28. uint32_t perm;
  29. void* impl;
  30. ino_t ino;
  31. vfs* fs;
  32. size_t size;
  33. };
  34. union node_t {
  35. uint32_t v;
  36. struct {
  37. uint32_t major : 16;
  38. uint32_t minor : 16;
  39. } in;
  40. };
  41. struct special_node;
  42. typedef size_t (*special_node_read)(special_node* sn, char* buf, size_t buf_size, size_t offset, size_t n);
  43. typedef size_t (*special_node_write)(special_node* sn, const char* buf, size_t offset, size_t n);
  44. struct special_node_ops {
  45. special_node_read read;
  46. special_node_write write;
  47. };
  48. struct special_node {
  49. special_node_ops ops;
  50. uint32_t data1;
  51. uint32_t data2;
  52. };
  53. struct stat {
  54. ino_t st_ino;
  55. node_t st_rdev;
  56. size_t st_size;
  57. blksize_t st_blksize;
  58. blkcnt_t st_blocks;
  59. };
  60. class vfs {
  61. public:
  62. struct dentry {
  63. public:
  64. using name_type = types::string<>;
  65. template <typename T>
  66. using allocator_type = types::kernel_allocator<T>;
  67. private:
  68. types::list<dentry, allocator_type>* children = nullptr;
  69. types::hash_map<name_type, dentry*, types::string_hasher<const name_type&>, allocator_type>* idx_children = nullptr;
  70. public:
  71. dentry* parent;
  72. inode* ind;
  73. // if the entry is a file, this flag is ignored
  74. union {
  75. uint32_t v;
  76. struct {
  77. uint32_t present : 1;
  78. uint32_t dirty : 1;
  79. } in;
  80. } flags;
  81. name_type name;
  82. explicit dentry(dentry* parent, inode* ind, const name_type& name);
  83. explicit dentry(dentry* parent, inode* ind, name_type&& name);
  84. dentry(const dentry& val) = delete;
  85. dentry(dentry&& val);
  86. dentry& operator=(const dentry& val) = delete;
  87. dentry& operator=(dentry&& val) = delete;
  88. ~dentry();
  89. dentry* append(inode* ind, const name_type& name);
  90. dentry* append(inode* ind, name_type&& name);
  91. dentry* find(const name_type& name);
  92. dentry* replace(dentry* val);
  93. void invalidate(void);
  94. };
  95. private:
  96. // TODO: use allocator designed for small objects
  97. using inode_list = types::list<inode>;
  98. using inode_index_cache_list = types::hash_map<ino_t, inode*, types::linux_hasher<ino_t>>;
  99. private:
  100. inode_list _inodes;
  101. inode_index_cache_list _idx_inodes;
  102. types::hash_map<dentry*, dentry*, types::linux_hasher<dentry*>> _mount_recover_list;
  103. ino_t _last_inode_no;
  104. private:
  105. ino_t _assign_inode_id(void);
  106. protected:
  107. dentry _root;
  108. protected:
  109. inode* cache_inode(inode_flags flags, uint32_t perm, size_t size, void* impl_data);
  110. inode* get_inode(ino_t ino);
  111. void register_root_node(inode* root);
  112. virtual int load_dentry(dentry* ent);
  113. public:
  114. explicit vfs(void);
  115. vfs(const vfs&) = delete;
  116. vfs& operator=(const vfs&) = delete;
  117. vfs(vfs&&) = delete;
  118. vfs& operator=(vfs&&) = delete;
  119. constexpr dentry* root(void)
  120. {
  121. return &_root;
  122. }
  123. int mount(dentry* mnt, vfs* new_fs);
  124. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  125. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  126. virtual int inode_mkfile(dentry* dir, const char* filename);
  127. virtual int inode_mknode(dentry* dir, const char* filename, union node_t sn);
  128. virtual int inode_rmfile(dentry* dir, const char* filename);
  129. virtual int inode_mkdir(dentry* dir, const char* dirname);
  130. virtual int inode_stat(dentry* dir, stat* stat);
  131. };
  132. extern fs::vfs::dentry* fs_root;
  133. void register_special_block(uint16_t major,
  134. uint16_t minor,
  135. special_node_read read,
  136. special_node_write write,
  137. uint32_t data1,
  138. uint32_t data2);
  139. vfs* register_fs(vfs* fs);
  140. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  141. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  142. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename);
  143. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, node_t sn);
  144. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  145. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname);
  146. int vfs_stat(const char* filename, stat* stat);
  147. int vfs_stat(fs::vfs::dentry* ent, stat* stat);
  148. // @return pointer to the dentry if found, nullptr if not
  149. fs::vfs::dentry* vfs_open(const char* path);
  150. } // namespace fs
  151. extern "C" void init_vfs(void);