vfs.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #include <map>
  3. #include <functional>
  4. #include <assert.h>
  5. #include <kernel/event/evtqueue.hpp>
  6. #include <stdint.h>
  7. #include <types/allocator.hpp>
  8. #include <types/buffer.hpp>
  9. #include <types/cplusplus.hpp>
  10. #include <types/hash_map.hpp>
  11. #include <types/list.hpp>
  12. #include <types/lock.hpp>
  13. #include <types/types.h>
  14. #include <types/vector.hpp>
  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. types::list<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, const name_type& name);
  108. explicit dentry(dentry* parent, inode* ind, name_type&& name);
  109. dentry(const dentry& val) = delete;
  110. constexpr dentry(dentry&& val)
  111. : children(std::exchange(val.children, nullptr))
  112. , idx_children(std::exchange(val.idx_children, nullptr))
  113. , parent(std::exchange(val.parent, nullptr))
  114. , ind(std::exchange(val.ind, nullptr))
  115. , flags { val.flags }
  116. , name(std::move(val.name))
  117. {
  118. if (children) {
  119. for (auto& item : *children)
  120. item.parent = this;
  121. }
  122. }
  123. dentry& operator=(const dentry& val) = delete;
  124. dentry& operator=(dentry&& val) = delete;
  125. constexpr ~dentry()
  126. {
  127. if (children) {
  128. types::pdelete<allocator_type>(children);
  129. children = nullptr;
  130. }
  131. if (idx_children) {
  132. types::pdelete<allocator_type>(idx_children);
  133. idx_children = nullptr;
  134. }
  135. }
  136. dentry* append(inode* ind, const name_type& name, bool set_dirty);
  137. dentry* append(inode* ind, name_type&& name, bool set_dirty);
  138. dentry* find(const name_type& name);
  139. dentry* replace(dentry* val);
  140. void invalidate(void);
  141. };
  142. public:
  143. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  144. private:
  145. // TODO: use allocator designed for small objects
  146. using inode_list = std::map<ino_t, inode>;
  147. private:
  148. inode_list _inodes;
  149. types::hash_map<dentry*, dentry*> _mount_recover_list;
  150. protected:
  151. dentry _root;
  152. protected:
  153. inode* cache_inode(inode_flags flags, uint32_t perm, size_t size, ino_t ino);
  154. inode* get_inode(ino_t ino);
  155. void register_root_node(inode* root);
  156. int load_dentry(dentry* ent);
  157. public:
  158. explicit vfs(void);
  159. vfs(const vfs&) = delete;
  160. vfs& operator=(const vfs&) = delete;
  161. vfs(vfs&&) = delete;
  162. vfs& operator=(vfs&&) = delete;
  163. constexpr dentry* root(void)
  164. {
  165. return &_root;
  166. }
  167. int mount(dentry* mnt, vfs* new_fs);
  168. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  169. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  170. virtual int inode_mkfile(dentry* dir, const char* filename);
  171. virtual int inode_mknode(dentry* dir, const char* filename, union node_t sn);
  172. virtual int inode_rmfile(dentry* dir, const char* filename);
  173. virtual int inode_mkdir(dentry* dir, const char* dirname);
  174. virtual int inode_stat(dentry* dir, stat* stat);
  175. virtual uint32_t inode_getnode(inode* file);
  176. // parameter 'length' in callback:
  177. // if 0, 'name' should be null terminated
  178. // else, 'name' size
  179. //
  180. // @return
  181. // return -1 if an error occurred
  182. // return 0 if no more entry available
  183. // otherwise, return bytes to be added to the offset
  184. virtual int inode_readdir(inode* dir, size_t offset, const filldir_func& callback) = 0;
  185. };
  186. class pipe : public types::non_copyable {
  187. private:
  188. static constexpr size_t PIPE_SIZE = 4096;
  189. static constexpr uint32_t READABLE = 1;
  190. static constexpr uint32_t WRITABLE = 2;
  191. private:
  192. types::buffer<types::kernel_allocator> buf;
  193. kernel::cond_var m_cv;
  194. uint32_t flags;
  195. public:
  196. pipe(void);
  197. void close_read(void);
  198. void close_write(void);
  199. int write(const char* buf, size_t n);
  200. int read(char* buf, size_t n);
  201. constexpr bool is_readable(void) const
  202. {
  203. return flags & READABLE;
  204. }
  205. constexpr bool is_writeable(void) const
  206. {
  207. return flags & WRITABLE;
  208. }
  209. constexpr bool is_free(void) const
  210. {
  211. return !(flags & (READABLE | WRITABLE));
  212. }
  213. };
  214. struct file {
  215. enum class types {
  216. ind,
  217. pipe,
  218. socket,
  219. } type;
  220. union {
  221. inode* ind;
  222. pipe* pp;
  223. } ptr;
  224. vfs::dentry* parent;
  225. size_t cursor;
  226. size_t ref;
  227. struct file_flags {
  228. uint32_t read : 1;
  229. uint32_t write : 1;
  230. } flags;
  231. };
  232. inline fs::vfs::dentry* fs_root;
  233. void register_special_block(uint16_t major,
  234. uint16_t minor,
  235. special_node_read read,
  236. special_node_write write,
  237. uint32_t data1,
  238. uint32_t data2);
  239. vfs* register_fs(vfs* fs);
  240. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  241. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  242. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename);
  243. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, node_t sn);
  244. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  245. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname);
  246. int vfs_stat(const char* filename, stat* stat);
  247. int vfs_stat(fs::vfs::dentry* ent, stat* stat);
  248. // @return pointer to the dentry if found, nullptr if not
  249. fs::vfs::dentry* vfs_open(const char* path);
  250. } // namespace fs
  251. extern "C" void init_vfs(void);