vfs.hpp 7.3 KB

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