VFS.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. extern struct vfs_superblock_t *vfs_root_sb;
  16. #define VFS_DPT_MBR 0 // MBR分区表
  17. #define VFS_DPT_GPT 1 // GPT分区表
  18. #define VFS_SUCCESS 0
  19. #define VFS_E_FS_EXISTED 1 // 错误:文件系统已存在
  20. #define VFS_E_FS_NOT_EXIST 2 // 错误:文件系统不存在
  21. /**
  22. * @brief 目录项的属性
  23. *
  24. */
  25. #define VFS_ATTR_FILE (1UL << 0)
  26. #define VFS_ATTR_DIR (1UL << 1)
  27. #define VFS_ATTR_DEVICE (1UL << 2)
  28. struct vfs_super_block_operations_t;
  29. struct vfs_inode_operations_t;
  30. struct vfs_index_node_t;
  31. struct vfs_dir_entry_operations_t;
  32. struct vfs_dir_entry_t
  33. {
  34. char *name;
  35. int name_length;
  36. struct List child_node_list;
  37. struct List subdirs_list;
  38. struct vfs_index_node_t *dir_inode;
  39. struct vfs_dir_entry_t *parent;
  40. struct vfs_dir_entry_operations_t *dir_ops;
  41. };
  42. struct vfs_superblock_t
  43. {
  44. struct vfs_dir_entry_t *root;
  45. struct vfs_super_block_operations_t *sb_ops;
  46. struct block_device *blk_device;
  47. void *private_sb_info;
  48. };
  49. /**
  50. * @brief inode结构体
  51. *
  52. */
  53. struct vfs_index_node_t
  54. {
  55. uint64_t file_size; // 文件大小
  56. uint64_t blocks; // 占用的扇区数
  57. uint64_t attribute;
  58. struct vfs_superblock_t *sb;
  59. struct vfs_file_operations_t *file_ops;
  60. struct vfs_inode_operations_t *inode_ops;
  61. void *private_inode_info;
  62. };
  63. /**
  64. * @brief 文件描述符
  65. *
  66. */
  67. struct vfs_file_t
  68. {
  69. long position;
  70. uint64_t mode;
  71. struct vfs_dir_entry_t *dEntry;
  72. struct vfs_file_operations_t *file_ops;
  73. void *private_data;
  74. };
  75. struct vfs_filesystem_type_t
  76. {
  77. char *name;
  78. int fs_flags;
  79. struct vfs_superblock_t *(*read_superblock)(struct block_device *blk); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。
  80. struct vfs_filesystem_type_t *next;
  81. };
  82. struct vfs_super_block_operations_t
  83. {
  84. void (*write_superblock)(struct vfs_superblock_t *sb); // 将超级块信息写入磁盘
  85. void (*put_superblock)(struct vfs_superblock_t *sb);
  86. void (*write_inode)(struct vfs_index_node_t *inode); // 将inode信息写入磁盘
  87. };
  88. /**
  89. * @brief 对vfs的inode的操作抽象
  90. *
  91. */
  92. struct vfs_inode_operations_t
  93. {
  94. /**
  95. * @brief 创建新的文件
  96. * @param parent_inode 父目录的inode结构体
  97. * @param dest_dEntry 新文件的dentry
  98. * @param mode 创建模式
  99. */
  100. long (*create)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode);
  101. /**
  102. * @brief 在文件系统中查找指定的目录项
  103. * @param parent_inode 父目录项(在这个目录下查找)
  104. * @param dest_dEntry 构造的目标目录项的结构体(传入名称,然后更多的详细信息将在本函数中完成填写)
  105. *
  106. */
  107. struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry);
  108. /**
  109. * @brief 创建文件夹
  110. * @param inode 父目录的inode
  111. * @param dEntry 新的文件夹的dentry
  112. * @param mode 创建文件夹的mode
  113. */
  114. long (*mkdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
  115. long (*rmdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry);
  116. 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);
  117. long (*getAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  118. long (*setAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr);
  119. };
  120. struct vfs_dir_entry_operations_t
  121. {
  122. long (*compare)(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename);
  123. long (*hash)(struct vfs_dir_entry_t *dEntry, char *filename);
  124. long (*release)(struct vfs_dir_entry_t *dEntry);
  125. long (*iput)(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode);
  126. };
  127. /**
  128. * @brief 填充dirent的函数指针的类型定义
  129. *
  130. */
  131. typedef int (*vfs_filldir_t)(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset);
  132. struct vfs_file_operations_t
  133. {
  134. long (*open)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  135. long (*close)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr);
  136. long (*read)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  137. long (*write)(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position);
  138. long (*lseek)(struct vfs_file_t *file_ptr, long offset, long origin);
  139. long (*ioctl)(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg);
  140. long (*readdir)(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler); // 读取文件夹
  141. };
  142. /**
  143. * @brief 在VFS中注册文件系统
  144. *
  145. * @param fs 文件系统类型结构体
  146. * @return uint64_t
  147. */
  148. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs);
  149. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs);
  150. /**
  151. * @brief 挂载文件系统
  152. *
  153. * @param path 要挂载到的路径
  154. * @param name 文件系统名
  155. * @param blk 块设备结构体
  156. * @return struct vfs_superblock_t* 挂载后,文件系统的超级块
  157. */
  158. struct vfs_superblock_t *vfs_mount_fs(const char *path, char *name, struct block_device *blk);
  159. /**
  160. * @brief 按照路径查找文件
  161. *
  162. * @param path 路径
  163. * @param flags 1:返回父目录项, 0:返回结果目录项
  164. * @return struct vfs_dir_entry_t* 目录项
  165. */
  166. struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags);
  167. /**
  168. * @brief 填充dentry
  169. *
  170. */
  171. int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset);