vfs.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <kernel/vfs/dentry.hpp>
  3. #include <kernel/vfs/file.hpp>
  4. #include <kernel/vfs/inode.hpp>
  5. #include <kernel/vfs/vfs.hpp>
  6. #include <stdint.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <bits/alltypes.h>
  10. #include <types/path.hpp>
  11. #define NODE_MAJOR(node) (((node) >> 8) & 0xFFU)
  12. #define NODE_MINOR(node) ((node) & 0xFFU)
  13. namespace fs {
  14. constexpr dev_t make_device(uint32_t major, uint32_t minor)
  15. {
  16. return ((major << 8) & 0xFF00U) | (minor & 0xFFU);
  17. }
  18. // buf, buf_size, offset, cnt
  19. using blkdev_read = std::function<ssize_t(char*, std::size_t, std::size_t, std::size_t)>;
  20. // buf, offset, cnt
  21. using blkdev_write = std::function<ssize_t(const char*, std::size_t, std::size_t)>;
  22. struct blkdev_ops {
  23. blkdev_read read;
  24. blkdev_write write;
  25. };
  26. // buf, buf_size, cnt
  27. using chrdev_read = std::function<ssize_t(char*, std::size_t, std::size_t)>;
  28. // buf, cnt
  29. using chrdev_write = std::function<ssize_t(const char*, std::size_t)>;
  30. struct chrdev_ops {
  31. chrdev_read read;
  32. chrdev_write write;
  33. };
  34. struct PACKED user_dirent {
  35. ino_t d_ino; // inode number
  36. uint32_t d_off; // ignored
  37. uint16_t d_reclen; // length of this struct user_dirent
  38. char d_name[1]; // file name with a padding zero
  39. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  40. };
  41. struct PACKED user_dirent64 {
  42. ino64_t d_ino; // inode number
  43. uint64_t d_off; // implementation-defined field, ignored
  44. uint16_t d_reclen; // length of this struct user_dirent
  45. uint8_t d_type; // file type, with offset of (d_reclen - 1)
  46. char d_name[1]; // file name with a padding zero
  47. };
  48. struct fs_context {
  49. dentry* root;
  50. };
  51. struct mount_data {
  52. fs::vfs* fs;
  53. std::string source;
  54. std::string mount_point;
  55. std::string fstype;
  56. unsigned long flags;
  57. };
  58. inline std::map<struct dentry*, mount_data> mounts;
  59. int register_block_device(dev_t node, const blkdev_ops& ops);
  60. int register_char_device(dev_t node, const chrdev_ops& ops);
  61. // return value: pointer to created vfs object
  62. // 1. const char*: source, such as "/dev/sda" or "proc"
  63. // 2. unsigned long: flags, such as MS_RDONLY | MS_RELATIME
  64. // 3. const void*: data, for filesystem use, such as "uid=1000"
  65. using create_fs_func_t = std::function<vfs*(const char*, unsigned long, const void*)>;
  66. int register_fs(const char* name, create_fs_func_t);
  67. // in tmpfs.cc
  68. int register_tmpfs();
  69. void partprobe();
  70. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset, size_t n);
  71. ssize_t block_device_write(dev_t node, const char* buf, size_t offset, size_t n);
  72. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  73. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  74. int creat(struct dentry* at, mode_t mode);
  75. int mkdir(struct dentry* at, mode_t mode);
  76. int mknod(struct dentry* at, mode_t mode, dev_t sn);
  77. int unlink(struct dentry* at);
  78. int symlink(struct dentry* at, const char* target);
  79. int statx(struct inode* inode, struct statx* stat, unsigned int mask);
  80. int readlink(struct inode* inode, char* buf, size_t buf_size);
  81. int truncate(struct inode* file, size_t size);
  82. size_t read(struct inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  83. size_t write(struct inode* file, const char* buf, size_t offset, size_t n);
  84. int mount(dentry* mnt, const char* source, const char* mount_point,
  85. const char* fstype, unsigned long flags, const void *data);
  86. std::pair<dentry*, int> open(const fs_context& context, dentry* cwd, types::path_iterator path,
  87. bool follow_symlinks = true, int recurs_no = 0);
  88. std::pair<dentry*, int> current_open(dentry* cwd, types::path_iterator path, bool follow_symlinks = true);
  89. } // namespace fs
  90. extern "C" void init_vfs(void);