vfs.hpp 7.9 KB

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