VFS.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "VFS.h"
  2. #include <common/kprint.h>
  3. #include <mm/slab.h>
  4. // 为filesystem_type_t结构体实例化一个链表头
  5. static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
  6. /**
  7. * @brief 挂载文件系统
  8. *
  9. * @param name 文件系统名
  10. * @param DPTE 分区表entry
  11. * @param DPT_type 分区表类型
  12. * @param buf 文件系统的引导扇区
  13. * @return struct vfs_superblock_t*
  14. */
  15. 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)
  16. {
  17. struct vfs_filesystem_type_t *p = NULL;
  18. for (p = &vfs_fs; p; p = p->next)
  19. {
  20. if (!strcmp(p->name, name)) // 存在符合的文件系统
  21. {
  22. return p->read_superblock(DPTE, DPT_type, buf, ahci_ctrl_num, ahci_port_num, part_num);
  23. }
  24. }
  25. kdebug("unsupported fs: %s", name);
  26. return NULL;
  27. }
  28. /**
  29. * @brief 在VFS中注册文件系统
  30. *
  31. * @param fs 文件系统类型结构体
  32. * @return uint64_t
  33. */
  34. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs)
  35. {
  36. struct vfs_filesystem_type_t *p = NULL;
  37. for (p = &vfs_fs; p; p = p->next)
  38. {
  39. if (!strcmp(p->name, fs->name)) // 已经注册相同名称的文件系统
  40. return VFS_E_FS_EXISTED;
  41. }
  42. fs->next = vfs_fs.next;
  43. vfs_fs.next = fs;
  44. return VFS_SUCCESS;
  45. }
  46. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs)
  47. {
  48. struct vfs_filesystem_type_t *p = &vfs_fs;
  49. while (p->next)
  50. {
  51. if (p->next == fs)
  52. {
  53. p->next = p->next->next;
  54. fs->next = NULL;
  55. return VFS_SUCCESS;
  56. }
  57. else
  58. p = p->next;
  59. }
  60. return VFS_E_FS_NOT_EXIST;
  61. }
  62. /**
  63. * @brief 按照路径查找文件
  64. *
  65. * @param path 路径
  66. * @param flags 1:返回父目录项, 0:返回结果目录项
  67. * @return struct vfs_dir_entry_t* 目录项
  68. */
  69. struct vfs_dir_entry_t *vfs_path_walk(char *path, uint64_t flags)
  70. {
  71. struct vfs_dir_entry_t *parent = vfs_root_sb->root;
  72. // 去除路径前的斜杠
  73. while (*path == '/')
  74. ++path;
  75. if ((!*path) || (*path == '\0'))
  76. return parent;
  77. struct vfs_dir_entry_t *dentry;
  78. while (true)
  79. {
  80. // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中
  81. char *tmp_path = path;
  82. while ((*path && *path != '\0') && (*path != '/'))
  83. ++path;
  84. int tmp_path_len = path - tmp_path;
  85. dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  86. memset(dentry, 0, sizeof(struct vfs_dir_entry_t));
  87. // 为目录项的名称分配内存
  88. dentry->name = (char *)kmalloc(tmp_path_len + 1, 0);
  89. // 貌似这里不需要memset,因为空间会被覆盖
  90. // memset(dentry->name, 0, tmp_path_len+1);
  91. memcpy(dentry->name, tmp_path, tmp_path_len);
  92. dentry->name[tmp_path_len] = '\0';
  93. dentry->name_length = tmp_path_len;
  94. if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
  95. {
  96. // 搜索失败
  97. kerror("cannot find the file/dir : %s", dentry->name);
  98. kfree(dentry->name);
  99. kfree(dentry);
  100. return NULL;
  101. }
  102. // 找到子目录项
  103. // 初始化子目录项的entry
  104. list_init(&dentry->child_node_list);
  105. list_init(&dentry->subdirs_list);
  106. dentry->parent = parent;
  107. while (*path == '/')
  108. ++path;
  109. if ((!*path) || (*path == '\0')) // 已经到达末尾
  110. {
  111. if (flags & 1) // 返回父目录
  112. {
  113. return parent;
  114. }
  115. return dentry;
  116. }
  117. parent = dentry;
  118. }
  119. }
  120. /**
  121. * @brief 填充dentry
  122. *
  123. */
  124. int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
  125. {
  126. }