vfs.hpp 7.5 KB

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