vfs.hpp 9.4 KB

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