vfs.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // out_dst SHOULD be empty
  140. void path(const dentry& root, name_type& out_dst) const;
  141. void invalidate(void);
  142. };
  143. public:
  144. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  145. private:
  146. // TODO: use allocator designed for small objects
  147. using inode_list = std::map<ino_t, inode>;
  148. private:
  149. inode_list _inodes;
  150. types::hash_map<dentry*, dentry*> _mount_recover_list;
  151. protected:
  152. dentry _root;
  153. protected:
  154. inode* cache_inode(inode_flags flags, uint32_t perm, size_t size, ino_t ino);
  155. inode* get_inode(ino_t ino);
  156. void register_root_node(inode* root);
  157. int load_dentry(dentry* ent);
  158. public:
  159. explicit vfs(void);
  160. vfs(const vfs&) = delete;
  161. vfs& operator=(const vfs&) = delete;
  162. vfs(vfs&&) = delete;
  163. vfs& operator=(vfs&&) = delete;
  164. constexpr dentry* root(void)
  165. {
  166. return &_root;
  167. }
  168. int mount(dentry* mnt, vfs* new_fs);
  169. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  170. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  171. virtual int inode_mkfile(dentry* dir, const char* filename);
  172. virtual int inode_mknode(dentry* dir, const char* filename, union node_t sn);
  173. virtual int inode_rmfile(dentry* dir, const char* filename);
  174. virtual int inode_mkdir(dentry* dir, const char* dirname);
  175. virtual int inode_stat(dentry* dir, stat* stat);
  176. virtual uint32_t inode_getnode(inode* file);
  177. // parameter 'length' in callback:
  178. // if 0, 'name' should be null terminated
  179. // else, 'name' size
  180. //
  181. // @return
  182. // return -1 if an error occurred
  183. // return 0 if no more entry available
  184. // otherwise, return bytes to be added to the offset
  185. virtual int inode_readdir(inode* dir, size_t offset, const filldir_func& callback) = 0;
  186. };
  187. class pipe : public types::non_copyable {
  188. private:
  189. static constexpr size_t PIPE_SIZE = 4096;
  190. static constexpr uint32_t READABLE = 1;
  191. static constexpr uint32_t WRITABLE = 2;
  192. private:
  193. types::buffer<types::kernel_allocator> buf;
  194. kernel::cond_var m_cv;
  195. uint32_t flags;
  196. public:
  197. pipe(void);
  198. void close_read(void);
  199. void close_write(void);
  200. int write(const char* buf, size_t n);
  201. int read(char* buf, size_t n);
  202. constexpr bool is_readable(void) const
  203. {
  204. return flags & READABLE;
  205. }
  206. constexpr bool is_writeable(void) const
  207. {
  208. return flags & WRITABLE;
  209. }
  210. constexpr bool is_free(void) const
  211. {
  212. return !(flags & (READABLE | WRITABLE));
  213. }
  214. };
  215. struct file {
  216. enum class types {
  217. ind,
  218. pipe,
  219. socket,
  220. } type;
  221. union {
  222. inode* ind;
  223. pipe* pp;
  224. } ptr;
  225. vfs::dentry* parent;
  226. size_t cursor;
  227. size_t ref;
  228. struct file_flags {
  229. uint32_t read : 1;
  230. uint32_t write : 1;
  231. } flags;
  232. };
  233. inline fs::vfs::dentry* fs_root;
  234. void register_special_block(uint16_t major,
  235. uint16_t minor,
  236. special_node_read read,
  237. special_node_write write,
  238. uint32_t data1,
  239. uint32_t data2);
  240. vfs* register_fs(vfs* fs);
  241. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  242. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  243. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename);
  244. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, node_t sn);
  245. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  246. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname);
  247. int vfs_stat(fs::vfs::dentry* ent, stat* stat);
  248. // @param: pwd: current working directory
  249. // if nullptr, use root directory
  250. // if present, must be a valid absolute path
  251. //
  252. // @return pointer to the dentry if found, nullptr if not
  253. fs::vfs::dentry* vfs_open(fs::vfs::dentry& root,
  254. const char* pwd, const char* path);
  255. } // namespace fs
  256. extern "C" void init_vfs(void);