vfs.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #pragma once
  2. #include <map>
  3. #include <list>
  4. #include <vector>
  5. #include <functional>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <bits/alltypes.h>
  11. #include <assert.h>
  12. #include <kernel/event/evtqueue.hpp>
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. #include <types/allocator.hpp>
  16. #include <types/buffer.hpp>
  17. #include <types/cplusplus.hpp>
  18. #include <types/hash_map.hpp>
  19. #include <types/path.hpp>
  20. #include <types/lock.hpp>
  21. #include <types/types.h>
  22. #define INODE_FILE (1 << 0)
  23. #define INODE_DIR (1 << 1)
  24. #define INODE_MNT (1 << 2)
  25. #define INODE_NODE (1 << 3)
  26. // dirent file types
  27. #define DT_UNKNOWN 0
  28. #define DT_FIFO 1
  29. #define DT_CHR 2
  30. #define DT_DIR 4
  31. #define DT_BLK 6
  32. #define DT_REG 8
  33. #define DT_LNK 10
  34. #define DT_SOCK 12
  35. #define DT_WHT 14
  36. #define DT_MAX (S_DT_MASK + 1) /* 16 */
  37. namespace fs {
  38. using blksize_t = size_t;
  39. using blkcnt_t = size_t;
  40. class vfs;
  41. struct inode {
  42. ino_t ino;
  43. vfs* fs;
  44. size_t size;
  45. nlink_t nlink;
  46. mode_t mode;
  47. uid_t uid;
  48. gid_t gid;
  49. };
  50. #define NODE_MAJOR(node) ((node) >> 16)
  51. #define NODE_MINOR(node) ((node) & 0xffff)
  52. constexpr dev_t NODE_INVALID = -1U;
  53. constexpr dev_t make_device(uint32_t major, uint32_t minor)
  54. {
  55. return (major << 16) | (minor & 0xffff);
  56. }
  57. // buf, buf_size, offset, cnt
  58. using blkdev_read = std::function<ssize_t(char*, std::size_t, std::size_t, std::size_t)>;
  59. // buf, offset, cnt
  60. using blkdev_write = std::function<ssize_t(const char*, std::size_t, std::size_t)>;
  61. struct blkdev_ops {
  62. blkdev_read read;
  63. blkdev_write write;
  64. };
  65. // buf, buf_size, cnt
  66. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  67. // buf, cnt
  68. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  69. struct chrdev_ops {
  70. chrdev_read read;
  71. chrdev_write write;
  72. };
  73. struct PACKED user_dirent {
  74. ino_t d_ino; // inode number
  75. uint32_t d_off; // ignored
  76. uint16_t d_reclen; // length of this struct user_dirent
  77. char d_name[1]; // file name with a padding zero
  78. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  79. };
  80. struct user_dirent64 {
  81. ino64_t d_ino; // inode number
  82. uint64_t d_off; // implementation-defined field, ignored
  83. uint16_t d_reclen; // length of this struct user_dirent
  84. uint8_t d_type; // file type, with offset of (d_reclen - 1)
  85. char d_name[1]; // file name with a padding zero
  86. };
  87. class vfs {
  88. public:
  89. struct dentry {
  90. public:
  91. using name_type = types::string<>;
  92. private:
  93. std::list<dentry>* children = nullptr;
  94. types::hash_map<name_type, dentry*>* idx_children = nullptr;
  95. public:
  96. dentry* parent;
  97. inode* ind;
  98. struct {
  99. uint32_t dir : 1; // whether the dentry is a directory.
  100. // if dir is 1, whether children contains valid data.
  101. // otherwise, ignored
  102. uint32_t present : 1;
  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, name_type name);
  134. dentry* find(const name_type& name);
  135. dentry* replace(dentry* val);
  136. void remove(const name_type& name);
  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. void free_inode(ino_t ino);
  154. inode* get_inode(ino_t ino);
  155. void register_root_node(inode* root);
  156. int load_dentry(dentry* ent);
  157. public:
  158. vfs();
  159. vfs(const vfs&) = delete;
  160. vfs& operator=(const vfs&) = delete;
  161. vfs(vfs&&) = delete;
  162. vfs& operator=(vfs&&) = delete;
  163. constexpr dentry* root(void)
  164. {
  165. return &_root;
  166. }
  167. int mount(dentry* mnt, vfs* new_fs);
  168. virtual size_t inode_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  169. virtual size_t inode_write(inode* file, const char* buf, size_t offset, size_t n);
  170. virtual int inode_mkfile(dentry* dir, const char* filename, mode_t mode);
  171. virtual int inode_mknode(dentry* dir, const char* filename, mode_t mode, dev_t sn);
  172. virtual int inode_rmfile(dentry* dir, const char* filename);
  173. virtual int inode_mkdir(dentry* dir, const char* dirname, mode_t mode);
  174. virtual int inode_statx(dentry* dent, statx* buf, unsigned int mask);
  175. virtual int inode_stat(dentry* dent, struct stat* stat);
  176. virtual dev_t inode_devid(inode* file);
  177. virtual int truncate(inode* file, size_t size);
  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 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 append : 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 do_write(const char* __user buf, size_t n) = 0;
  229. virtual off_t seek(off_t n, int whence)
  230. { return (void)n, (void)whence, -ESPIPE; }
  231. ssize_t write(const char* __user buf, size_t n)
  232. {
  233. if (!flags.write)
  234. return -EBADF;
  235. if (flags.append) {
  236. seek(0, SEEK_END);
  237. }
  238. return do_write(buf, n);
  239. }
  240. // regular files should override this method
  241. virtual int getdents(char* __user buf, size_t cnt)
  242. { return (void)buf, (void)cnt, -ENOTDIR; }
  243. virtual int getdents64(char* __user buf, size_t cnt)
  244. { return (void)buf, (void)cnt, -ENOTDIR; }
  245. };
  246. struct regular_file : public virtual file {
  247. virtual ~regular_file() = default;
  248. std::size_t cursor { };
  249. inode* ind { };
  250. regular_file(vfs::dentry* parent, file_flags flags, size_t cursor, inode* ind);
  251. virtual ssize_t read(char* __user buf, size_t n) override;
  252. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  253. virtual off_t seek(off_t n, int whence) override;
  254. virtual int getdents(char* __user buf, size_t cnt) override;
  255. virtual int getdents64(char* __user buf, size_t cnt) override;
  256. };
  257. struct fifo_file : public virtual file {
  258. virtual ~fifo_file() override;
  259. std::shared_ptr<pipe> ppipe;
  260. fifo_file(vfs::dentry* parent, file_flags flags, std::shared_ptr<fs::pipe> ppipe);
  261. virtual ssize_t read(char* __user buf, size_t n) override;
  262. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  263. };
  264. inline fs::vfs::dentry* fs_root;
  265. int register_block_device(dev_t node, blkdev_ops ops);
  266. int register_char_device(dev_t node, chrdev_ops ops);
  267. void partprobe();
  268. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset, size_t n);
  269. ssize_t block_device_write(dev_t node, const char* buf, size_t offset, size_t n);
  270. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  271. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  272. vfs* register_fs(vfs* fs);
  273. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  274. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  275. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename, mode_t mode);
  276. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, mode_t mode, dev_t sn);
  277. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  278. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname, mode_t mode);
  279. int vfs_stat(fs::vfs::dentry* dent, statx* stat, unsigned int mask);
  280. int vfs_truncate(inode* file, size_t size);
  281. /**
  282. * @brief Opens a file or directory specified by the given path.
  283. *
  284. * @param root The root directory of the file system.
  285. * @param path The absolute path to the file or directory to be opened.
  286. * @return A pointer to the opened file or directory entry if found.
  287. * Otherwise, nullptr is returned.
  288. */
  289. fs::vfs::dentry* vfs_open(fs::vfs::dentry& root, const types::path& path);
  290. } // namespace fs
  291. extern "C" void init_vfs(void);