vfs.hpp 9.6 KB

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