vfs.hpp 8.1 KB

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