VFS.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file VFS.h
  3. * @author fslongjin (longjin@RinGoTek.cn)
  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. #include <common/fcntl.h>
  14. #include <common/blk_types.h>
  15. #include <mm/slab.h>
  16. extern struct vfs_superblock_t *vfs_root_sb;
  17. #define VFS_DPT_MBR 0 // MBR分区表
  18. #define VFS_DPT_GPT 1 // GPT分区表
  19. #define VFS_SUCCESS 0
  20. #define VFS_E_FS_EXISTED 1 // 错误:文件系统已存在
  21. #define VFS_E_FS_NOT_EXIST 2 // 错误:文件系统不存在
  22. #define VFS_MAX_PATHLEN 1024
  23. /**
  24. * @brief 目录项的属性
  25. *
  26. */
  27. #define VFS_ATTR_FILE (1UL << 0)
  28. #define VFS_ATTR_DIR (1UL << 1)
  29. #define VFS_ATTR_DEVICE (1UL << 2)
  30. struct vfs_super_block_operations_t;
  31. struct vfs_inode_operations_t;
  32. struct vfs_index_node_t;
  33. struct vfs_dir_entry_operations_t;
  34. struct vfs_dir_entry_t
  35. {
  36. char *name;
  37. int name_length;
  38. struct List child_node_list;
  39. struct List subdirs_list;
  40. struct vfs_index_node_t *dir_inode;
  41. struct vfs_dir_entry_t *parent;
  42. struct vfs_dir_entry_operations_t *dir_ops;
  43. };
  44. struct vfs_superblock_t
  45. {
  46. struct vfs_dir_entry_t *root;
  47. struct vfs_super_block_operations_t *sb_ops;
  48. struct vfs_dir_entry_operations_t *dir_ops; // dentry's operations
  49. struct block_device *blk_device;
  50. void *private_sb_info;
  51. };
  52. /**
  53. * @brief inode结构体
  54. *
  55. */
  56. struct vfs_index_node_t
  57. {
  58. uint64_t file_size; // 文件大小
  59. uint64_t blocks; // 占用的扇区数
  60. uint64_t attribute;
  61. struct vfs_superblock_t *sb;
  62. struct vfs_file_operations_t *file_ops;
  63. struct vfs_inode_operations_t *inode_ops;
  64. void *private_inode_info;
  65. };
  66. /**
  67. * @brief 文件描述符
  68. *
  69. */
  70. struct vfs_file_t
  71. {
  72. long position;
  73. uint64_t mode;
  74. struct vfs_dir_entry_t *dEntry;
  75. struct vfs_file_operations_t *file_ops;
  76. void *private_data;
  77. };
  78. struct vfs_filesystem_type_t
  79. {
  80. char *name;
  81. int fs_flags;
  82. struct vfs_superblock_t *(*read_superblock)(struct block_device *blk); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。
  83. struct vfs_filesystem_type_t *next;
  84. };
  85. struct vfs_super_block_operations_t
  86. {
  87. void (*write_superblock)(struct vfs_superblock_t *sb); // 将超级块信息写入磁盘
  88. void (*put_superblock)(struct vfs_superblock_t *sb);
  89. void (*write_inode)(struct vfs_index_node_t *inode); // 将inode信息写入磁盘
  90. };
  91. /**
  92. * @brief 对vfs的inode的操作抽象
  93. *
  94. */
  95. struct vfs_inode_operations_t
  96. {
  97. /**
  98. * @brief 创建新的文件
  99. * @param parent_inode 父目录的inode结构体
  100. * @param dest_dEntry 新文件的dentry
  101. * @param mode 创建模式
  102. */
  103. long (*create)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode);
  104. /**
  105. * @brief 在文件系统中查找指定的目录项
  106. * @param parent_inode 父目录项(在这个目录下查找)
  107. * @param dest_dEntry 构造的目标目录项的结构体(传入名称,然后更多的详细信息将在本函数中完成填写)
  108. *
  109. */
  110. struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry);
  111. /**
  112. * @brief 创建文件夹
  113. * @param inode 父目录的inode
  114. * @param dEntry 新的文件夹的dentry
  115. * @param mode 创建文件夹的mode
  116. */
  117. long (*mkdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
  118. long (*rmdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry);
  119. 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);
  120. long (*getAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  121. long (*setAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  122. };
  123. struct vfs_dir_entry_operations_t
  124. {
  125. long (*compare)(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename);
  126. long (*hash)(struct vfs_dir_entry_t *dEntry, char *filename);
  127. long (*release)(struct vfs_dir_entry_t *dEntry);
  128. long (*iput)(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode);
  129. };
  130. /**
  131. * @brief 填充dirent的函数指针的类型定义
  132. *
  133. */
  134. typedef int (*vfs_filldir_t)(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset);
  135. struct vfs_file_operations_t
  136. {
  137. long (*open)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  138. long (*close)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  139. long (*read)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  140. long (*write)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  141. long (*lseek)(struct vfs_file_t *file_ptr, long offset, long origin);
  142. long (*ioctl)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg);
  143. long (*readdir)(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler); // 读取文件夹
  144. };
  145. /**
  146. * @brief 在VFS中注册文件系统
  147. *
  148. * @param fs 文件系统类型结构体
  149. * @return uint64_t
  150. */
  151. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs);
  152. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs);
  153. /**
  154. * @brief 挂载文件系统
  155. *
  156. * @param path 要挂载到的路径
  157. * @param name 文件系统名
  158. * @param blk 块设备结构体
  159. * @return struct vfs_superblock_t* 挂载后,文件系统的超级块
  160. */
  161. struct vfs_superblock_t *vfs_mount_fs(const char *path, char *name, struct block_device *blk);
  162. /**
  163. * @brief 按照路径查找文件
  164. *
  165. * @param path 路径
  166. * @param flags 1:返回父目录项, 0:返回结果目录项
  167. * @return struct vfs_dir_entry_t* 目录项
  168. */
  169. struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags);
  170. /**
  171. * @brief 填充dentry
  172. *
  173. */
  174. int vfs_fill_dirent(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset);
  175. /**
  176. * @brief 初始化vfs
  177. *
  178. * @return int 错误码
  179. */
  180. int vfs_init();
  181. /**
  182. * @brief 动态分配dentry以及路径字符串名称
  183. *
  184. * @param name_size 名称字符串大小(字节)(注意考虑字符串最后需要有一个‘\0’作为结尾)
  185. * @return struct vfs_dir_entry_t* 创建好的dentry
  186. */
  187. struct vfs_dir_entry_t *vfs_alloc_dentry(const int name_size);
  188. /**
  189. * @brief 打开文件
  190. *
  191. * @param filename 文件路径
  192. * @param flags 标志位
  193. * @return uint64_t 错误码
  194. */
  195. uint64_t do_open(const char *filename, int flags);
  196. /**
  197. * @brief 创建文件夹
  198. *
  199. * @param path 文件夹路径
  200. * @param mode 创建模式
  201. * @param from_userland 该创建请求是否来自用户态
  202. * @return int64_t 错误码
  203. */
  204. int64_t vfs_mkdir(const char* path, mode_t mode, bool from_userland);