VFS.h 7.8 KB

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