vfs.hpp 6.9 KB

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