vfs.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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) >> 16)
  12. #define NODE_MINOR(node) ((node) & 0xffffU)
  13. namespace fs {
  14. constexpr dev_t make_device(uint32_t major, uint32_t minor)
  15. {
  16. return (major << 16) | (minor & 0xffff);
  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 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. inline dentry* fs_root;
  49. int register_block_device(dev_t node, blkdev_ops ops);
  50. int register_char_device(dev_t node, chrdev_ops ops);
  51. // return value: pointer to created vfs object
  52. // 1. dev_t: device number
  53. using create_fs_func_t = std::function<vfs*(dev_t)>;
  54. int register_fs(const char* name, create_fs_func_t);
  55. // in tmpfs.cc
  56. int register_tmpfs();
  57. // returns a pointer to the vfs object
  58. // vfs objects are managed by the kernel
  59. int create_fs(const char* name, dev_t device, vfs*& out_vfs);
  60. void partprobe();
  61. ssize_t block_device_read(dev_t node, char* buf, size_t buf_size, size_t offset, size_t n);
  62. ssize_t block_device_write(dev_t node, const char* buf, size_t offset, size_t n);
  63. ssize_t char_device_read(dev_t node, char* buf, size_t buf_size, size_t n);
  64. ssize_t char_device_write(dev_t node, const char* buf, size_t n);
  65. size_t vfs_read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n);
  66. size_t vfs_write(inode* file, const char* buf, size_t offset, size_t n);
  67. int vfs_mkfile(dentry* dir, const char* filename, mode_t mode);
  68. int vfs_mknode(dentry* dir, const char* filename, mode_t mode, dev_t sn);
  69. int vfs_rmfile(dentry* dir, const char* filename);
  70. int vfs_mkdir(dentry* dir, const char* dirname, mode_t mode);
  71. int vfs_stat(dentry* dent, statx* stat, unsigned int mask);
  72. int vfs_truncate(inode* file, size_t size);
  73. /**
  74. * @brief Opens a file or directory specified by the given path.
  75. *
  76. * @param root The root directory of the file system.
  77. * @param path The absolute path to the file or directory to be opened.
  78. * @return A pointer to the opened file or directory entry if found.
  79. * Otherwise, nullptr is returned.
  80. */
  81. dentry* vfs_open(dentry& root, const types::path& path);
  82. } // namespace fs
  83. extern "C" void init_vfs(void);