vfs.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #pragma once
  2. #include <stdint.h>
  3. #include <types/allocator.hpp>
  4. #include <types/function.hpp>
  5. #include <types/hash_map.hpp>
  6. #include <types/list.hpp>
  7. #include <types/map.hpp>
  8. #include <types/types.h>
  9. #include <types/vector.hpp>
  10. #define INODE_FILE (1 << 0)
  11. #define INODE_DIR (1 << 1)
  12. #define INODE_MNT (1 << 2)
  13. #define INODE_NODE (1 << 3)
  14. // dirent file types
  15. #define DT_UNKNOWN 0
  16. #define DT_FIFO 1
  17. #define DT_CHR 2
  18. #define DT_DIR 4
  19. #define DT_BLK 6
  20. #define DT_REG 8
  21. #define DT_LNK 10
  22. #define DT_SOCK 12
  23. #define DT_WHT 14
  24. #define DT_MAX (S_DT_MASK + 1) /* 16 */
  25. namespace fs {
  26. using ino_t = size_t;
  27. using blksize_t = size_t;
  28. using blkcnt_t = size_t;
  29. class vfs;
  30. union inode_flags {
  31. uint32_t v;
  32. struct {
  33. uint32_t file : 1;
  34. uint32_t directory : 1;
  35. uint32_t mount_point : 1;
  36. uint32_t special_node : 1;
  37. } in;
  38. };
  39. struct inode {
  40. inode_flags flags;
  41. uint32_t perm;
  42. ino_t ino;
  43. vfs* fs;
  44. size_t size;
  45. };
  46. #define SN_INVALID (0xffffffff)
  47. union node_t {
  48. uint32_t v;
  49. struct {
  50. uint32_t major : 16;
  51. uint32_t minor : 16;
  52. } in;
  53. };
  54. struct special_node;
  55. typedef size_t (*special_node_read)(special_node* sn, char* buf, size_t buf_size, size_t offset, size_t n);
  56. typedef size_t (*special_node_write)(special_node* sn, const char* buf, size_t offset, size_t n);
  57. struct special_node_ops {
  58. special_node_read read;
  59. special_node_write write;
  60. };
  61. struct special_node {
  62. special_node_ops ops;
  63. uint32_t data1;
  64. uint32_t data2;
  65. };
  66. struct stat {
  67. ino_t st_ino;
  68. node_t st_rdev;
  69. size_t st_size;
  70. blksize_t st_blksize;
  71. blkcnt_t st_blocks;
  72. };
  73. struct PACKED user_dirent {
  74. ino_t d_ino; // inode number
  75. uint32_t d_off; // ignored
  76. uint16_t d_reclen; // length of this struct user_dirent
  77. char d_name[1]; // file name with a padding zero
  78. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  79. };
  80. class vfs {
  81. public:
  82. struct dentry {
  83. public:
  84. using name_type = types::string<>;
  85. template <typename T>
  86. using allocator_type = types::kernel_allocator<T>;
  87. private:
  88. types::list<dentry, allocator_type>* children = nullptr;
  89. types::hash_map<name_type, dentry*, types::string_hasher<const name_type&>, allocator_type>* idx_children = nullptr;
  90. public:
  91. dentry* parent;
  92. inode* ind;
  93. // if the entry is a file, this flag is ignored
  94. union {
  95. uint32_t v;
  96. struct {
  97. uint32_t present : 1;
  98. uint32_t dirty : 1;
  99. } in;
  100. } flags;
  101. name_type name;
  102. explicit dentry(dentry* parent, inode* ind, const name_type& name);
  103. explicit dentry(dentry* parent, inode* ind, name_type&& name);
  104. dentry(const dentry& val) = delete;
  105. dentry(dentry&& val);
  106. dentry& operator=(const dentry& val) = delete;
  107. dentry& operator=(dentry&& val) = delete;
  108. ~dentry();
  109. dentry* append(inode* ind, const name_type& name, bool set_dirty);
  110. dentry* append(inode* ind, name_type&& name, bool set_dirty);
  111. dentry* find(const name_type& name);
  112. dentry* replace(dentry* val);
  113. void invalidate(void);
  114. };
  115. public:
  116. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  117. private:
  118. // TODO: use allocator designed for small objects
  119. using inode_list = types::map<ino_t, inode>;
  120. private:
  121. inode_list _inodes;
  122. types::hash_map<dentry*, dentry*, types::linux_hasher<dentry*>> _mount_recover_list;
  123. protected:
  124. dentry _root;
  125. protected:
  126. inode* cache_inode(inode_flags flags, uint32_t perm, size_t size, ino_t ino);
  127. inode* get_inode(ino_t ino);
  128. void register_root_node(inode* root);
  129. int load_dentry(dentry* ent);
  130. public:
  131. explicit vfs(void);
  132. vfs(const vfs&) = delete;
  133. vfs& operator=(const vfs&) = delete;
  134. vfs(vfs&&) = delete;
  135. vfs& operator=(vfs&&) = delete;
  136. constexpr dentry* root(void)
  137. {
  138. return &_root;
  139. }
  140. int mount(dentry* mnt, vfs* new_fs);
  141. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  142. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  143. virtual int inode_mkfile(dentry* dir, const char* filename);
  144. virtual int inode_mknode(dentry* dir, const char* filename, union node_t sn);
  145. virtual int inode_rmfile(dentry* dir, const char* filename);
  146. virtual int inode_mkdir(dentry* dir, const char* dirname);
  147. virtual int inode_stat(dentry* dir, stat* stat);
  148. virtual uint32_t inode_getnode(inode* file);
  149. // parameter 'length' in callback:
  150. // if 0, 'name' should be null terminated
  151. // else, 'name' size
  152. //
  153. // @return
  154. // return -1 if an error occurred
  155. // return 0 if no more entry available
  156. // otherwise, return bytes to be added to the offset
  157. virtual int inode_readdir(inode* dir, size_t offset, filldir_func callback) = 0;
  158. };
  159. struct file {
  160. enum class types {
  161. regular_file,
  162. directory,
  163. block_dev,
  164. char_dev,
  165. } type;
  166. inode* ind;
  167. vfs::dentry* parent;
  168. size_t cursor;
  169. size_t ref;
  170. };
  171. inline fs::vfs::dentry* fs_root;
  172. void register_special_block(uint16_t major,
  173. uint16_t minor,
  174. special_node_read read,
  175. special_node_write write,
  176. uint32_t data1,
  177. uint32_t data2);
  178. vfs* register_fs(vfs* fs);
  179. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  180. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  181. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename);
  182. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, node_t sn);
  183. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  184. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname);
  185. int vfs_stat(const char* filename, stat* stat);
  186. int vfs_stat(fs::vfs::dentry* ent, stat* stat);
  187. // @return pointer to the dentry if found, nullptr if not
  188. fs::vfs::dentry* vfs_open(const char* path);
  189. } // namespace fs
  190. extern "C" void init_vfs(void);