vfs.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include <types/string.hpp>
  23. #define NODE_MAJOR(node) ((node) >> 16)
  24. #define NODE_MINOR(node) ((node) & 0xffffU)
  25. namespace fs {
  26. class vfs;
  27. struct inode {
  28. ino_t ino;
  29. vfs* fs;
  30. size_t size;
  31. nlink_t nlink;
  32. mode_t mode;
  33. uid_t uid;
  34. gid_t gid;
  35. };
  36. constexpr dev_t make_device(uint32_t major, uint32_t minor)
  37. {
  38. return (major << 16) | (minor & 0xffff);
  39. }
  40. // buf, buf_size, offset, cnt
  41. using blkdev_read = std::function<ssize_t(char*, std::size_t, std::size_t, std::size_t)>;
  42. // buf, offset, cnt
  43. using blkdev_write = std::function<ssize_t(const char*, std::size_t, std::size_t)>;
  44. struct blkdev_ops {
  45. blkdev_read read;
  46. blkdev_write write;
  47. };
  48. // buf, buf_size, cnt
  49. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  50. // buf, cnt
  51. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  52. struct chrdev_ops {
  53. chrdev_read read;
  54. chrdev_write write;
  55. };
  56. struct PACKED user_dirent {
  57. ino_t d_ino; // inode number
  58. uint32_t d_off; // ignored
  59. uint16_t d_reclen; // length of this struct user_dirent
  60. char d_name[1]; // file name with a padding zero
  61. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  62. };
  63. struct user_dirent64 {
  64. ino64_t d_ino; // inode number
  65. uint64_t d_off; // implementation-defined field, ignored
  66. uint16_t d_reclen; // length of this struct user_dirent
  67. uint8_t d_type; // file type, with offset of (d_reclen - 1)
  68. char d_name[1]; // file name with a padding zero
  69. };
  70. class vfs {
  71. public:
  72. struct dentry {
  73. public:
  74. using name_type = types::string<>;
  75. private:
  76. std::list<dentry>* children = nullptr;
  77. types::hash_map<name_type, dentry*>* idx_children = nullptr;
  78. public:
  79. dentry* parent;
  80. inode* ind;
  81. struct {
  82. uint32_t dir : 1; // whether the dentry is a directory.
  83. // if dir is 1, whether children contains valid data.
  84. // otherwise, ignored
  85. uint32_t present : 1;
  86. } flags;
  87. name_type name;
  88. explicit dentry(dentry* parent, inode* ind, name_type name);
  89. dentry(const dentry& val) = delete;
  90. constexpr dentry(dentry&& val)
  91. : children(std::exchange(val.children, nullptr))
  92. , idx_children(std::exchange(val.idx_children, nullptr))
  93. , parent(std::exchange(val.parent, nullptr))
  94. , ind(std::exchange(val.ind, nullptr))
  95. , flags { val.flags }
  96. , name(std::move(val.name))
  97. {
  98. if (children) {
  99. for (auto& item : *children)
  100. item.parent = this;
  101. }
  102. }
  103. dentry& operator=(const dentry& val) = delete;
  104. dentry& operator=(dentry&& val) = delete;
  105. constexpr ~dentry()
  106. {
  107. if (children) {
  108. delete children;
  109. children = nullptr;
  110. }
  111. if (idx_children) {
  112. delete idx_children;
  113. idx_children = nullptr;
  114. }
  115. }
  116. dentry* append(inode* ind, name_type name);
  117. dentry* find(const name_type& name);
  118. dentry* replace(dentry* val);
  119. void remove(const name_type& name);
  120. // out_dst SHOULD be empty
  121. void path(const dentry& root, types::path& out_dst) const;
  122. };
  123. public:
  124. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  125. private:
  126. // TODO: use allocator designed for small objects
  127. using inode_list = std::map<ino_t, inode>;
  128. private:
  129. inode_list _inodes;
  130. types::hash_map<dentry*, dentry*> _mount_recover_list;
  131. protected:
  132. dentry _root;
  133. protected:
  134. inode* cache_inode(size_t size, ino_t ino, mode_t mode, uid_t uid, gid_t gid);
  135. void free_inode(ino_t ino);
  136. inode* get_inode(ino_t ino);
  137. void register_root_node(inode* root);
  138. int load_dentry(dentry* ent);
  139. public:
  140. vfs();
  141. vfs(const vfs&) = delete;
  142. vfs& operator=(const vfs&) = delete;
  143. vfs(vfs&&) = delete;
  144. vfs& operator=(vfs&&) = delete;
  145. constexpr dentry* root(void)
  146. {
  147. return &_root;
  148. }
  149. int mount(dentry* mnt, vfs* new_fs);
  150. // directory operations
  151. virtual int inode_mkfile(dentry* dir, const char* filename, mode_t mode);
  152. virtual int inode_mknode(dentry* dir, const char* filename, mode_t mode, dev_t sn);
  153. virtual int inode_rmfile(dentry* dir, const char* filename);
  154. virtual int inode_mkdir(dentry* dir, const char* dirname, mode_t mode);
  155. // metadata operation
  156. virtual int inode_statx(dentry* dent, statx* buf, unsigned int mask);
  157. virtual int inode_stat(dentry* dent, struct stat* stat);
  158. // file operations
  159. virtual size_t read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  160. virtual size_t write(inode* file, const char* buf, size_t offset, size_t n);
  161. virtual int dev_id(inode* file, dev_t& out_dev);
  162. virtual int truncate(inode* file, size_t size);
  163. // parameter 'length' in callback:
  164. // if 0, 'name' should be null terminated
  165. // else, 'name' size
  166. //
  167. // @return
  168. // return -1 if an error occurred
  169. // return 0 if no more entry available
  170. // otherwise, return bytes to be added to the offset
  171. virtual int inode_readdir(inode* dir, size_t offset, const filldir_func& callback) = 0;
  172. };
  173. class pipe : public types::non_copyable {
  174. private:
  175. static constexpr size_t PIPE_SIZE = 4096;
  176. static constexpr uint32_t READABLE = 1;
  177. static constexpr uint32_t WRITABLE = 2;
  178. private:
  179. types::buffer buf;
  180. kernel::cond_var m_cv;
  181. uint32_t flags;
  182. public:
  183. pipe(void);
  184. void close_read(void);
  185. void close_write(void);
  186. int write(const char* buf, size_t n);
  187. int read(char* buf, size_t n);
  188. constexpr bool is_readable(void) const
  189. {
  190. return flags & READABLE;
  191. }
  192. constexpr bool is_writeable(void) const
  193. {
  194. return flags & WRITABLE;
  195. }
  196. constexpr bool is_free(void) const
  197. {
  198. return !(flags & (READABLE | WRITABLE));
  199. }
  200. };
  201. struct file {
  202. mode_t mode; // stores the file type in the same format as inode::mode
  203. vfs::dentry* parent {};
  204. struct file_flags {
  205. uint32_t read : 1;
  206. uint32_t write : 1;
  207. uint32_t append : 1;
  208. } flags {};
  209. file(mode_t mode, vfs::dentry* parent, file_flags flags)
  210. : mode(mode) , parent(parent), flags(flags) { }
  211. virtual ~file() = default;
  212. virtual ssize_t read(char* __user buf, size_t n) = 0;
  213. virtual ssize_t do_write(const char* __user buf, size_t n) = 0;
  214. virtual off_t seek(off_t n, int whence)
  215. { return (void)n, (void)whence, -ESPIPE; }
  216. ssize_t write(const char* __user buf, size_t n)
  217. {
  218. if (!flags.write)
  219. return -EBADF;
  220. if (flags.append) {
  221. seek(0, SEEK_END);
  222. }
  223. return do_write(buf, n);
  224. }
  225. // regular files should override this method
  226. virtual int getdents(char* __user buf, size_t cnt)
  227. { return (void)buf, (void)cnt, -ENOTDIR; }
  228. virtual int getdents64(char* __user buf, size_t cnt)
  229. { return (void)buf, (void)cnt, -ENOTDIR; }
  230. };
  231. struct regular_file : public virtual file {
  232. virtual ~regular_file() = default;
  233. std::size_t cursor { };
  234. inode* ind { };
  235. regular_file(vfs::dentry* parent, file_flags flags, size_t cursor, inode* ind);
  236. virtual ssize_t read(char* __user buf, size_t n) override;
  237. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  238. virtual off_t seek(off_t n, int whence) 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 do_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. // return value: pointer to created vfs object
  253. // 1. dev_t: device number
  254. using create_fs_func_t = std::function<vfs*(dev_t)>;
  255. int register_fs(const char* name, create_fs_func_t);
  256. // in tmpfs.cc
  257. int register_tmpfs();
  258. // returns a pointer to the vfs object
  259. // vfs objects are managed by the kernel
  260. int create_fs(const char* name, dev_t device, vfs*& out_vfs);
  261. void partprobe();
  262. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset, size_t n);
  263. ssize_t block_device_write(dev_t node, const char* buf, size_t offset, size_t n);
  264. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  265. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  266. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  267. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  268. int vfs_mkfile(fs::vfs::dentry* dir, const char* filename, mode_t mode);
  269. int vfs_mknode(fs::vfs::dentry* dir, const char* filename, mode_t mode, dev_t sn);
  270. int vfs_rmfile(fs::vfs::dentry* dir, const char* filename);
  271. int vfs_mkdir(fs::vfs::dentry* dir, const char* dirname, mode_t mode);
  272. int vfs_stat(fs::vfs::dentry* dent, statx* stat, unsigned int mask);
  273. int vfs_truncate(inode* file, size_t size);
  274. /**
  275. * @brief Opens a file or directory specified by the given path.
  276. *
  277. * @param root The root directory of the file system.
  278. * @param path The absolute path to the file or directory to be opened.
  279. * @return A pointer to the opened file or directory entry if found.
  280. * Otherwise, nullptr is returned.
  281. */
  282. fs::vfs::dentry* vfs_open(fs::vfs::dentry& root, const types::path& path);
  283. } // namespace fs
  284. extern "C" void init_vfs(void);