VFS.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "VFS.h"
  2. #include <common/kprint.h>
  3. #include <common/dirent.h>
  4. #include <common/string.h>
  5. #include <common/errno.h>
  6. #include <mm/mm.h>
  7. #include <mm/slab.h>
  8. #include <process/ptrace.h>
  9. #include <process/process.h>
  10. // 为filesystem_type_t结构体实例化一个链表头
  11. static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
  12. /**
  13. * @brief 挂载文件系统
  14. *
  15. * @param name 文件系统名
  16. * @param blk 块设备结构体
  17. * @return struct vfs_superblock_t*
  18. */
  19. struct vfs_superblock_t *vfs_mount_fs(char *name, struct block_device *blk)
  20. {
  21. struct vfs_filesystem_type_t *p = NULL;
  22. for (p = &vfs_fs; p; p = p->next)
  23. {
  24. if (!strcmp(p->name, name)) // 存在符合的文件系统
  25. {
  26. return p->read_superblock(blk);
  27. }
  28. }
  29. kdebug("unsupported fs: %s", name);
  30. return NULL;
  31. }
  32. /**
  33. * @brief 在VFS中注册文件系统
  34. *
  35. * @param fs 文件系统类型结构体
  36. * @return uint64_t
  37. */
  38. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs)
  39. {
  40. struct vfs_filesystem_type_t *p = NULL;
  41. for (p = &vfs_fs; p; p = p->next)
  42. {
  43. if (!strcmp(p->name, fs->name)) // 已经注册相同名称的文件系统
  44. return VFS_E_FS_EXISTED;
  45. }
  46. fs->next = vfs_fs.next;
  47. vfs_fs.next = fs;
  48. return VFS_SUCCESS;
  49. }
  50. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs)
  51. {
  52. struct vfs_filesystem_type_t *p = &vfs_fs;
  53. while (p->next)
  54. {
  55. if (p->next == fs)
  56. {
  57. p->next = p->next->next;
  58. fs->next = NULL;
  59. return VFS_SUCCESS;
  60. }
  61. else
  62. p = p->next;
  63. }
  64. return VFS_E_FS_NOT_EXIST;
  65. }
  66. /**
  67. * @brief 按照路径查找文件
  68. *
  69. * @param path 路径
  70. * @param flags 1:返回父目录项, 0:返回结果目录项
  71. * @return struct vfs_dir_entry_t* 目录项
  72. */
  73. struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags)
  74. {
  75. struct vfs_dir_entry_t *parent = vfs_root_sb->root;
  76. // 去除路径前的斜杠
  77. while (*path == '/')
  78. ++path;
  79. if ((!*path) || (*path == '\0'))
  80. return parent;
  81. struct vfs_dir_entry_t *dentry;
  82. // kdebug("path before walk:%s", path);
  83. while (true)
  84. {
  85. // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中
  86. const char *tmp_path = path;
  87. while ((*path && *path != '\0') && (*path != '/'))
  88. ++path;
  89. int tmp_path_len = path - tmp_path;
  90. dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  91. memset(dentry, 0, sizeof(struct vfs_dir_entry_t));
  92. // 为目录项的名称分配内存
  93. dentry->name = (char *)kmalloc(tmp_path_len + 1, 0);
  94. // 貌似这里不需要memset,因为空间会被覆盖
  95. // memset(dentry->name, 0, tmp_path_len+1);
  96. memcpy(dentry->name, (void *)tmp_path, tmp_path_len);
  97. dentry->name[tmp_path_len] = '\0';
  98. // kdebug("tmp_path_len=%d, dentry->name= %s", tmp_path_len, dentry->name);
  99. dentry->name_length = tmp_path_len;
  100. if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
  101. {
  102. // 搜索失败
  103. // kerror("cannot find the file/dir : %s", dentry->name);
  104. kfree(dentry->name);
  105. kfree(dentry);
  106. return NULL;
  107. }
  108. // 找到子目录项
  109. // 初始化子目录项的entry
  110. list_init(&dentry->child_node_list);
  111. list_init(&dentry->subdirs_list);
  112. dentry->parent = parent;
  113. list_add(&parent->subdirs_list, &dentry->child_node_list);
  114. while (*path == '/')
  115. ++path;
  116. if ((!*path) || (*path == '\0')) // 已经到达末尾
  117. {
  118. if (flags & 1) // 返回父目录
  119. {
  120. return parent;
  121. }
  122. return dentry;
  123. }
  124. parent = dentry;
  125. }
  126. }
  127. /**
  128. * @brief 填充dentry
  129. *
  130. */
  131. int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
  132. {
  133. struct dirent *dent = (struct dirent *)buf;
  134. // 如果尝试访问内核空间,则返回错误
  135. if (!(verify_area((uint64_t)buf, sizeof(struct dirent) + namelen)))
  136. return -EFAULT;
  137. // ====== 填充dirent结构体 =====
  138. memset(buf, 0, sizeof(struct dirent) + namelen);
  139. memcpy(dent->d_name, name, namelen);
  140. dent->d_name[namelen] = '\0';
  141. // 暂时不显示目录下的记录数
  142. dent->d_reclen = 0;
  143. dent->d_ino = d_ino;
  144. dent->d_off = offset;
  145. dent->d_type = type;
  146. // 返回dirent的总大小
  147. return sizeof(struct dirent) + namelen;
  148. }
  149. /**
  150. * @brief 创建文件夹
  151. *
  152. * @param path(r8) 路径
  153. * @param mode(r9) 模式
  154. * @return uint64_t
  155. */
  156. uint64_t sys_mkdir(struct pt_regs *regs)
  157. {
  158. const char *path = (const char *)regs->r8;
  159. // kdebug("path = %s", path);
  160. mode_t mode = (mode_t)regs->r9;
  161. uint32_t pathlen;
  162. if (regs->cs & USER_CS)
  163. pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
  164. else
  165. pathlen = strnlen(path, PAGE_4K_SIZE - 1);
  166. if (pathlen == 0)
  167. return -ENOENT;
  168. int last_slash = -1;
  169. // 查找最后一个'/',忽略路径末尾的'/'
  170. for (int i = pathlen - 2; i >= 0; --i)
  171. {
  172. if (path[i] == '/')
  173. {
  174. last_slash = i;
  175. break;
  176. }
  177. }
  178. // 路径格式不合法(必须使用绝对路径)
  179. if (last_slash < 0)
  180. return ENOTDIR;
  181. char *buf = (char *)kmalloc(last_slash + 1, 0);
  182. memset(buf, 0, pathlen + 1);
  183. // 拷贝字符串(不包含要被创建的部分)
  184. if (regs->cs & USER_CS)
  185. strncpy_from_user(buf, path, last_slash);
  186. else
  187. strncpy(buf, path, last_slash);
  188. buf[last_slash + 1] = '\0';
  189. // kdebug("to walk: %s", buf);
  190. // 查找父目录
  191. struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0);
  192. if (parent_dir == NULL)
  193. {
  194. kwarn("parent dir is NULL.");
  195. kfree(buf);
  196. return -ENOENT;
  197. }
  198. kfree(buf);
  199. // 检查父目录中是否已经有相同的目录项
  200. if (vfs_path_walk((const char *)path, 0) != NULL)
  201. {
  202. // 目录中已有对应的文件夹
  203. kwarn("Dir '%s' aleardy exists.", path);
  204. return -EEXIST;
  205. }
  206. struct vfs_dir_entry_t *subdir_dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  207. memset((void *)subdir_dentry, 0, sizeof(struct vfs_dir_entry_t));
  208. if (path[pathlen - 1] == '/')
  209. subdir_dentry->name_length = pathlen - last_slash - 2;
  210. else
  211. subdir_dentry->name_length = pathlen - last_slash - 1;
  212. subdir_dentry->name = (char *)kmalloc(subdir_dentry->name_length + 1, 0);
  213. memset((void *)subdir_dentry->name, 0, subdir_dentry->name_length + 1);
  214. for (int i = last_slash + 1, cnt = 0; i < pathlen && cnt < subdir_dentry->name_length; ++i, ++cnt)
  215. {
  216. subdir_dentry->name[cnt] = path[i];
  217. }
  218. ++subdir_dentry->name_length;
  219. // kdebug("last_slash=%d", last_slash);
  220. // kdebug("name=%s", path + last_slash + 1);
  221. subdir_dentry->parent = parent_dir;
  222. // kdebug("to mkdir, parent name=%s", parent_dir->name);
  223. int retval = parent_dir->dir_inode->inode_ops->mkdir(parent_dir->dir_inode, subdir_dentry, 0);
  224. list_add(&parent_dir->subdirs_list, &subdir_dentry->child_node_list);
  225. // kdebug("retval = %d", retval);
  226. return 0;
  227. }