vfs.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. mode_t mode;
  46. uid_t uid;
  47. gid_t gid;
  48. };
  49. #define NODE_MAJOR(node) ((node) >> 16)
  50. #define NODE_MINOR(node) ((node) & 0xffff)
  51. constexpr dev_t NODE_INVALID = -1U;
  52. constexpr dev_t make_device(uint32_t major, uint32_t minor)
  53. {
  54. return (major << 16) | (minor & 0xffff);
  55. }
  56. // buf, buf_size, offset, cnt
  57. using blkdev_read = std::function<ssize_t(char*, std::size_t, std::size_t, std::size_t)>;
  58. // buf, offset, cnt
  59. using blkdev_write = std::function<ssize_t(const char*, std::size_t, std::size_t)>;
  60. struct blkdev_ops {
  61. blkdev_read read;
  62. blkdev_write write;
  63. };
  64. // buf, buf_size, cnt
  65. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  66. // buf, cnt
  67. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  68. struct chrdev_ops {
  69. chrdev_read read;
  70. chrdev_write write;
  71. };
  72. struct PACKED user_dirent {
  73. ino_t d_ino; // inode number
  74. uint32_t d_off; // ignored
  75. uint16_t d_reclen; // length of this struct user_dirent
  76. char d_name[1]; // file name with a padding zero
  77. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  78. };
  79. struct user_dirent64 {
  80. ino64_t d_ino; // inode number
  81. uint64_t d_off; // implementation-defined field, ignored
  82. uint16_t d_reclen; // length of this struct user_dirent
  83. uint8_t d_type; // file type, with offset of (d_reclen - 1)
  84. char d_name[1]; // file name with a padding zero
  85. };
  86. class vfs {
  87. public:
  88. struct dentry {
  89. public:
  90. using name_type = types::string<>;
  91. private:
  92. std::list<dentry>* children = nullptr;
  93. types::hash_map<name_type, dentry*>* idx_children = nullptr;
  94. public:
  95. dentry* parent;
  96. inode* ind;
  97. // if the entry is a file, this flag is ignored
  98. union {
  99. uint32_t v;
  100. struct {
  101. uint32_t present : 1;
  102. uint32_t dirty : 1;
  103. } in;
  104. } flags;
  105. name_type name;
  106. explicit dentry(dentry* parent, inode* ind, name_type name);
  107. dentry(const dentry& val) = delete;
  108. constexpr dentry(dentry&& val)
  109. : children(std::exchange(val.children, nullptr))
  110. , idx_children(std::exchange(val.idx_children, nullptr))
  111. , parent(std::exchange(val.parent, nullptr))
  112. , ind(std::exchange(val.ind, nullptr))
  113. , flags { val.flags }
  114. , name(std::move(val.name))
  115. {
  116. if (children) {
  117. for (auto& item : *children)
  118. item.parent = this;
  119. }
  120. }
  121. dentry& operator=(const dentry& val) = delete;
  122. dentry& operator=(dentry&& val) = delete;
  123. constexpr ~dentry()
  124. {
  125. if (children) {
  126. delete children;
  127. children = nullptr;
  128. }
  129. if (idx_children) {
  130. delete idx_children;
  131. idx_children = nullptr;
  132. }
  133. }
  134. dentry* append(inode* ind, const name_type& name, bool set_dirty);
  135. dentry* append(inode* ind, name_type&& name, bool set_dirty);
  136. dentry* find(const name_type& name);
  137. dentry* replace(dentry* val);
  138. // out_dst SHOULD be empty
  139. void path(const dentry& root, types::path& out_dst) const;
  140. void invalidate(void);
  141. };
  142. public:
  143. using filldir_func = std::function<int(const char*, size_t, ino_t, uint8_t)>;
  144. private:
  145. // TODO: use allocator designed for small objects
  146. using inode_list = std::map<ino_t, inode>;
  147. private:
  148. inode_list _inodes;
  149. types::hash_map<dentry*, dentry*> _mount_recover_list;
  150. protected:
  151. dentry _root;
  152. protected:
  153. inode* cache_inode(size_t size, ino_t ino, mode_t mode, uid_t uid, gid_t gid);
  154. inode* get_inode(ino_t ino);
  155. void register_root_node(inode* root);
  156. int load_dentry(dentry* ent);
  157. public:
  158. explicit vfs(void);
  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);