VFS.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file VFS.h
  3. * @author fslongjin ([email protected])
  4. * @brief 虚拟文件系统
  5. * @version 0.1
  6. * @date 2022-04-20
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #pragma once
  12. #include <common/glib.h>
  13. struct vfs_superblock_t *vfs_root_sb = NULL;
  14. #define VFS_DPT_MBR 0 // MBR分区表
  15. #define VFS_DPT_GPT 1 // GPT分区表
  16. #define VFS_SUCCESS 0
  17. #define VFS_E_FS_EXISTED 1 // 错误:文件系统已存在
  18. #define VFS_E_FS_NOT_EXIST 2 // 错误:文件系统不存在
  19. /**
  20. * @brief 目录项的属性
  21. *
  22. */
  23. #define VFS_ATTR_FILE (1UL << 0)
  24. #define VFS_ATTR_DIR (1UL << 1)
  25. struct vfs_super_block_operations_t;
  26. struct vfs_inode_operations_t;
  27. struct vfs_index_node_t;
  28. struct vfs_dir_entry_operations_t;
  29. struct vfs_dir_entry_t
  30. {
  31. char *name;
  32. int name_length;
  33. struct List child_node_list;
  34. struct List subdirs_list;
  35. struct vfs_index_node_t *dir_inode;
  36. struct vfs_dir_entry_t *parent;
  37. struct vfs_dir_entry_operations_t *dir_ops;
  38. };
  39. struct vfs_superblock_t
  40. {
  41. struct vfs_dir_entry_t *root;
  42. struct vfs_super_block_operations_t *sb_ops;
  43. void *private_sb_info;
  44. };
  45. /**
  46. * @brief inode结构体
  47. *
  48. */
  49. struct vfs_index_node_t
  50. {
  51. uint64_t file_size; // 文件大小
  52. uint64_t blocks; // 占用的扇区数
  53. uint64_t attribute;
  54. struct vfs_superblock_t *sb;
  55. struct vfs_file_operations_t *file_ops;
  56. struct vfs_inode_operations_t *inode_ops;
  57. void *private_inode_info;
  58. };
  59. /**
  60. * @brief 文件描述符
  61. *
  62. */
  63. struct vfs_file_t
  64. {
  65. long position;
  66. uint64_t mode;
  67. struct vfs_dir_entry_t *dEntry;
  68. struct vfs_file_operations_t *file_ops;
  69. void *private_data;
  70. };
  71. struct vfs_filesystem_type_t
  72. {
  73. char *name;
  74. int fs_flags;
  75. struct vfs_superblock_t *(*read_superblock)(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。其中DPTE为磁盘分区表entry(MBR、GPT不同)
  76. struct vfs_filesystem_type_t *next;
  77. };
  78. struct vfs_super_block_operations_t
  79. {
  80. void (*write_superblock)(struct vfs_superblock_t *sb); // 将超级块信息写入磁盘
  81. void (*put_superblock)(struct vfs_superblock_t *sb);
  82. void (*write_inode)(struct vfs_index_node_t *inode); // 将inode信息写入磁盘
  83. };
  84. /**
  85. * @brief 对vfs的inode的操作抽象
  86. *
  87. */
  88. struct vfs_inode_operations_t
  89. {
  90. long (*create)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
  91. struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry);
  92. long (*mkdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
  93. long (*rmdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry);
  94. long (*rename)(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry);
  95. long (*getAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  96. long (*setAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  97. };
  98. struct vfs_dir_entry_operations_t
  99. {
  100. long (*compare)(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename);
  101. long (*hash)(struct vfs_dir_entry_t *dEntry, char *filename);
  102. long (*release)(struct vfs_dir_entry_t *dEntry);
  103. long (*iput)(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode);
  104. };
  105. struct vfs_file_operations_t
  106. {
  107. long (*open)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  108. long (*close)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  109. long (*read)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  110. long (*write)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  111. long (*lseek)(struct vfs_file_t *file_ptr, long offset, long origin);
  112. long (*ioctl)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg);
  113. };
  114. /**
  115. * @brief 在VFS中注册文件系统
  116. *
  117. * @param fs 文件系统类型结构体
  118. * @return uint64_t
  119. */
  120. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs);
  121. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs);
  122. /**
  123. * @brief 挂载文件系统
  124. *
  125. * @param name 文件系统名
  126. * @param DPTE 分区表entry
  127. * @param DPT_type 分区表类型
  128. * @param buf 文件系统的引导扇区
  129. * @return struct vfs_superblock_t*
  130. */
  131. struct vfs_superblock_t *vfs_mount_fs(char *name, void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num);
  132. /**
  133. * @brief 按照路径查找文件
  134. *
  135. * @param path 路径
  136. * @param flags 1:返回父目录项, 0:返回结果目录项
  137. * @return struct vfs_dir_entry_t* 目录项
  138. */
  139. struct vfs_dir_entry_t *vfs_path_walk(char *path, uint64_t flags);