vfs.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. template <typename T>
  91. using allocator_type = types::kernel_allocator<T>;
  92. private:
  93. std::list<dentry, types::allocator_adapter<dentry, allocator_type>>* children = nullptr;
  94. types::hash_map<name_type, dentry*, types::linux_hasher, 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, name_type name);
  108. dentry(const dentry& val) = delete;
  109. constexpr dentry(dentry&& val)
  110. : children(std::exchange(val.children, nullptr))
  111. , idx_children(std::exchange(val.idx_children, nullptr))
  112. , parent(std::exchange(val.parent, nullptr))
  113. , ind(std::exchange(val.ind, nullptr))
  114. , flags { val.flags }
  115. , name(std::move(val.name))
  116. {
  117. if (children) {
  118. for (auto& item : *children)
  119. item.parent = this;
  120. }
  121. }
  122. dentry& operator=(const dentry& val) = delete;
  123. dentry& operator=(dentry&& val) = delete;
  124. constexpr ~dentry()
  125. {
  126. if (children) {
  127. types::pdelete<allocator_type>(children);
  128. children = nullptr;
  129. }
  130. if (idx_children) {
  131. types::pdelete<allocator_type>(idx_children);
  132. idx_children = nullptr;
  133. }
  134. }
  135. dentry* append(inode* ind, const name_type& name, bool set_dirty);
  136. dentry* append(inode* ind, name_type&& name, bool set_dirty);
  137. dentry* find(const name_type& name);
  138. dentry* replace(dentry* val);
  139. // out_dst SHOULD be empty
  140. void path(const dentry& root, types::path& out_dst) const;
  141. void invalidate(void);
  142. };
  143. public:
  144. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  145. private:
  146. // TODO: use allocator designed for small objects
  147. using inode_list = std::map<ino_t, inode>;
  148. private:
  149. inode_list _inodes;
  150. types::hash_map<dentry*, dentry*> _mount_recover_list;
  151. protected:
  152. dentry _root;
  153. protected:
  154. inode* cache_inode(size_t size, ino_t ino, mode_t mode, uid_t uid, gid_t gid);
  155. inode* get_inode(ino_t ino);
  156. void register_root_node(inode* root);
  157. int load_dentry(dentry* ent);
  158. public:
  159. explicit vfs(void);
  160. vfs(const vfs&) = delete;
  161. vfs& operator=(const vfs&) = delete;
  162. vfs(vfs&&) = delete;
  163. vfs& operator=(vfs&&) = delete;
  164. constexpr dentry* root(void)
  165. {
  166. return &_root;
  167. }
  168. int mount(dentry* mnt, vfs* new_fs);
  169. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  170. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  171. virtual int inode_mkfile(dentry* dir, const char* filename, mode_t mode);
  172. virtual int inode_mknode(dentry* dir, const char* filename, mode_t mode, dev_t sn);
  173. virtual int inode_rmfile(dentry* dir, const char* filename);
  174. virtual int inode_mkdir(dentry* dir, const char* dirname);
  175. virtual int inode_statx(dentry* dent, statx* buf, unsigned int mask);
  176. virtual int inode_stat(dentry* dent, struct stat* stat);
  177. virtual dev_t inode_devid(inode* file);
  178. // parameter 'length' in callback:
  179. // if 0, 'name' should be null terminated
  180. // else, 'name' size
  181. //
  182. // @return
  183. // return -1 if an error occurred
  184. // return 0 if no more entry available
  185. // otherwise, return bytes to be added to the offset
  186. virtual int inode_readdir(inode* dir, size_t offset, const filldir_func& callback) = 0;
  187. };
  188. class pipe : public types::non_copyable {
  189. private:
  190. static constexpr size_t PIPE_SIZE = 4096;
  191. static constexpr uint32_t READABLE = 1;
  192. static constexpr uint32_t WRITABLE = 2;
  193. private:
  194. types::buffer<types::kernel_allocator> buf;
  195. kernel::cond_var m_cv;
  196. uint32_t flags;
  197. public:
  198. pipe(void);
  199. void close_read(void);
  200. void close_write(void);
  201. int write(const char* buf, size_t n);
  202. int read(char* buf, size_t n);
  203. constexpr bool is_readable(void) const
  204. {
  205. return flags & READABLE;
  206. }
  207. constexpr bool is_writeable(void) const
  208. {
  209. return flags & WRITABLE;
  210. }
  211. constexpr bool is_free(void) const
  212. {
  213. return !(flags & (READABLE | WRITABLE));
  214. }
  215. };
  216. struct file {
  217. mode_t mode; // stores the file type in the same format as inode::mode
  218. vfs::dentry* parent {};
  219. struct file_flags {
  220. uint32_t read : 1;
  221. uint32_t write : 1;
  222. uint32_t close_on_exec : 1;
  223. } flags {};
  224. file(mode_t mode, vfs::dentry* parent, file_flags flags)
  225. : mode(mode) , parent(parent), flags(flags) { }
  226. virtual ~file() = default;
  227. virtual ssize_t read(char* __user buf, size_t n) = 0;
  228. virtual ssize_t write(const char* __user buf, size_t n) = 0;
  229. virtual void close() = 0;
  230. // regular files should override this method
  231. virtual int getdents(char* __user buf, size_t cnt)
  232. { return (void)buf, (void)cnt, -ENOTDIR; }
  233. virtual int getdents64(char* __user buf, size_t cnt)
  234. { return (void)buf, (void)cnt, -ENOTDIR; }
  235. };
  236. struct regular_file : public virtual file {
  237. virtual ~regular_file() = default;
  238. std::size_t cursor { };
  239. inode* ind { };
  240. regular_file(vfs::dentry* parent, file_flags flags, size_t cursor, inode* ind);
  241. virtual ssize_t read(char* __user buf, size_t n) override;
  242. virtual ssize_t write(const char* __user buf, size_t n) override;
  243. virtual void close() override;
  244. virtual int getdents(char* __user buf, size_t cnt) override;
  245. virtual int getdents64(char* __user buf, size_t cnt) override;
  246. };
  247. struct fifo_file : public virtual file {
  248. virtual ~fifo_file() = default;
  249. std::shared_ptr<pipe> ppipe;
  250. fifo_file(vfs::dentry* parent, file_flags flags, std::shared_ptr<fs::pipe> ppipe);
  251. virtual ssize_t read(char* __user buf, size_t n) override;
  252. virtual ssize_t write(const char* __user buf, size_t n) override;
  253. virtual void close() override;
  254. };
  255. inline fs::vfs::dentry* fs_root;
  256. int register_block_device(dev_t node, blkdev_ops ops);
  257. int register_char_device(dev_t node, chrdev_ops ops);
  258. void partprobe();
  259. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset, size_t n);
  260. ssize_t block_device_write(dev_t node, const char* buf, size_t offset, size_t n);
  261. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  262. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  263. vfs* register_fs(vfs* fs);
  264. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  265. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  266. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename, mode_t mode);
  267. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, mode_t mode, dev_t sn);
  268. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  269. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname);
  270. int vfs_stat(fs::vfs::dentry* dent, statx* stat, unsigned int mask);
  271. /**
  272. * @brief Opens a file or directory specified by the given path.
  273. *
  274. * @param root The root directory of the file system.
  275. * @param path The absolute path to the file or directory to be opened.
  276. * @return A pointer to the opened file or directory entry if found.
  277. * Otherwise, nullptr is returned.
  278. */
  279. fs::vfs::dentry* vfs_open(fs::vfs::dentry& root, const types::path& path);
  280. } // namespace fs
  281. extern "C" void init_vfs(void);