VFS.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. #include "VFS.h"
  2. #include "mount.h"
  3. #include "internal.h"
  4. #include <common/kprint.h>
  5. #include <debug/bug.h>
  6. #include <common/dirent.h>
  7. #include <common/string.h>
  8. #include <common/errno.h>
  9. #include <mm/mm.h>
  10. #include <mm/slab.h>
  11. #include <process/ptrace.h>
  12. #include <process/process.h>
  13. #include <filesystem/rootfs/rootfs.h>
  14. // 为filesystem_type_t结构体实例化一个链表头
  15. static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
  16. struct vfs_superblock_t *vfs_root_sb = NULL;
  17. struct vfs_dir_entry_t *vfs_alloc_dentry(const int name_size);
  18. /**
  19. * @brief 挂载文件系统
  20. *
  21. * @param path 要挂载到的路径
  22. * @param name 文件系统名
  23. * @param blk 块设备结构体
  24. * @return struct vfs_superblock_t* 挂载后,文件系统的超级块
  25. */
  26. struct vfs_superblock_t *vfs_mount_fs(const char *path, char *name, struct block_device *blk)
  27. {
  28. // 判断挂载点是否存在
  29. struct vfs_dir_entry_t *target_dentry = NULL;
  30. target_dentry = vfs_path_walk(path, 0);
  31. if (target_dentry == NULL)
  32. return NULL;
  33. struct vfs_filesystem_type_t *p = NULL;
  34. for (p = &vfs_fs; p; p = p->next)
  35. {
  36. if (!strcmp(p->name, name)) // 存在符合的文件系统
  37. {
  38. struct vfs_superblock_t *sb = p->read_superblock(blk);
  39. if (strcmp(path, "/") == 0) // 如果挂载到的是'/'挂载点,则让其成为最顶层的文件系统
  40. {
  41. vfs_root_sb = sb;
  42. }
  43. else
  44. {
  45. kdebug("to mount %s", name);
  46. // 调用mount机制,挂载文件系统
  47. struct vfs_dir_entry_t *new_dentry = sb->root;
  48. // 注意,umount的时候需要释放这些内存
  49. new_dentry->name = kzalloc(target_dentry->name_length + 1, 0);
  50. new_dentry->name_length = target_dentry->name_length;
  51. do_mount(target_dentry, new_dentry);
  52. }
  53. return sb;
  54. }
  55. }
  56. kdebug("unsupported fs: %s", name);
  57. return NULL;
  58. }
  59. /**
  60. * @brief 在VFS中注册文件系统
  61. *
  62. * @param fs 文件系统类型结构体
  63. * @return uint64_t
  64. */
  65. uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs)
  66. {
  67. struct vfs_filesystem_type_t *p = NULL;
  68. for (p = &vfs_fs; p; p = p->next)
  69. {
  70. if (!strcmp(p->name, fs->name)) // 已经注册相同名称的文件系统
  71. return VFS_E_FS_EXISTED;
  72. }
  73. fs->next = vfs_fs.next;
  74. vfs_fs.next = fs;
  75. return VFS_SUCCESS;
  76. }
  77. uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs)
  78. {
  79. struct vfs_filesystem_type_t *p = &vfs_fs;
  80. while (p->next)
  81. {
  82. if (p->next == fs)
  83. {
  84. p->next = p->next->next;
  85. fs->next = NULL;
  86. return VFS_SUCCESS;
  87. }
  88. else
  89. p = p->next;
  90. }
  91. return VFS_E_FS_NOT_EXIST;
  92. }
  93. /**
  94. * @brief 在dentry的sub_dir_list中搜索指定名称的dentry
  95. *
  96. * @param dentry 目录项结构体dentry
  97. * @param name 待搜索的dentry名称
  98. * @return struct vfs_dir_entry_t* 目标dentry (无结果则返回NULL)
  99. */
  100. static struct vfs_dir_entry_t *vfs_search_dentry_list(struct vfs_dir_entry_t *dentry, const char *name)
  101. {
  102. if (list_empty(&dentry->subdirs_list))
  103. return NULL;
  104. struct List *ptr = &dentry->subdirs_list;
  105. struct vfs_dir_entry_t *d_ptr = NULL;
  106. do
  107. {
  108. ptr = list_next(ptr);
  109. d_ptr = container_of(ptr, struct vfs_dir_entry_t, child_node_list);
  110. if (strcmp(name, d_ptr->name) == 0)
  111. return d_ptr;
  112. } while (list_next(ptr) != (&dentry->subdirs_list));
  113. return NULL;
  114. }
  115. /**
  116. * @brief 按照路径查找文件
  117. *
  118. * @param path 路径
  119. * @param flags 1:返回父目录项, 0:返回结果目录项
  120. * @return struct vfs_dir_entry_t* 目录项
  121. */
  122. struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags)
  123. {
  124. struct vfs_dir_entry_t *parent = vfs_root_sb->root;
  125. // 去除路径前的斜杠
  126. while (*path == '/')
  127. ++path;
  128. if ((!*path) || (*path == '\0'))
  129. return parent;
  130. struct vfs_dir_entry_t *dentry = NULL;
  131. // kdebug("path before walk:%s", path);
  132. while (true)
  133. {
  134. // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中
  135. const char *tmp_path = path;
  136. while ((*path && *path != '\0') && (*path != '/'))
  137. ++path;
  138. int tmp_path_len = path - tmp_path;
  139. // 搜索是否有dentry缓存
  140. {
  141. char *tmpname = kzalloc(tmp_path_len + 1, 0);
  142. strncpy(tmpname, tmp_path, tmp_path_len);
  143. tmpname[tmp_path_len] = '\0';
  144. dentry = vfs_search_dentry_list(parent, tmpname);
  145. kfree(tmpname);
  146. }
  147. // 如果没有找到dentry缓存,则申请新的dentry
  148. if (dentry == NULL)
  149. {
  150. dentry = vfs_alloc_dentry(tmp_path_len + 1);
  151. memcpy(dentry->name, (void *)tmp_path, tmp_path_len);
  152. dentry->name[tmp_path_len] = '\0';
  153. // kdebug("tmp_path_len=%d, dentry->name= %s", tmp_path_len, dentry->name);
  154. dentry->name_length = tmp_path_len;
  155. if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
  156. {
  157. // 搜索失败
  158. // kerror("cannot find the file/dir : %s", dentry->name);
  159. kfree(dentry->name);
  160. kfree(dentry);
  161. return NULL;
  162. }
  163. // 找到子目录项
  164. dentry->parent = parent;
  165. list_add(&parent->subdirs_list, &dentry->child_node_list);
  166. }
  167. while (*path == '/')
  168. ++path;
  169. if ((!*path) || (*path == '\0')) // 已经到达末尾
  170. {
  171. if (flags & 1) // 返回父目录
  172. {
  173. return parent;
  174. }
  175. return dentry;
  176. }
  177. parent = dentry;
  178. }
  179. }
  180. /**
  181. * @brief 填充dentry
  182. *
  183. * @return dirent的总大小
  184. */
  185. int vfs_fill_dirent(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
  186. {
  187. struct dirent *dent = (struct dirent *)buf;
  188. // 如果尝试访问内核空间,则返回错误
  189. if (!(verify_area((uint64_t)buf, sizeof(struct dirent) + namelen)))
  190. return -EFAULT;
  191. // ====== 填充dirent结构体 =====
  192. memset(buf, 0, sizeof(struct dirent) + namelen);
  193. memcpy(dent->d_name, name, namelen);
  194. dent->d_name[namelen] = '\0';
  195. // 暂时不显示目录下的记录数
  196. dent->d_reclen = 0;
  197. dent->d_ino = d_ino;
  198. dent->d_off = offset;
  199. dent->d_type = type;
  200. // 返回dirent的总大小
  201. return sizeof(struct dirent) + namelen;
  202. }
  203. /**
  204. * @brief 创建文件夹
  205. *
  206. * @param path 文件夹路径
  207. * @param mode 创建模式
  208. * @param from_userland 该创建请求是否来自用户态
  209. * @return int64_t 错误码
  210. */
  211. int64_t vfs_mkdir(const char *path, mode_t mode, bool from_userland)
  212. {
  213. uint32_t pathlen;
  214. if (from_userland)
  215. pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
  216. else
  217. pathlen = strnlen(path, PAGE_4K_SIZE - 1);
  218. if (pathlen == 0)
  219. return -ENOENT;
  220. int last_slash = -1;
  221. // 查找最后一个'/',忽略路径末尾的'/'
  222. for (int i = pathlen - 2; i >= 0; --i)
  223. {
  224. if (path[i] == '/')
  225. {
  226. last_slash = i;
  227. break;
  228. }
  229. }
  230. // 路径格式不合法(必须使用绝对路径)
  231. if (last_slash < 0)
  232. return -ENOTDIR;
  233. char *buf = (char *)kzalloc(last_slash + 2, 0);
  234. // 拷贝字符串(不包含要被创建的部分)
  235. if (from_userland)
  236. strncpy_from_user(buf, path, last_slash);
  237. else
  238. strncpy(buf, path, last_slash);
  239. buf[last_slash + 1] = '\0';
  240. // 查找父目录
  241. struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0);
  242. if (parent_dir == NULL)
  243. {
  244. kwarn("parent dir is NULL.");
  245. kfree(buf);
  246. return -ENOENT;
  247. }
  248. kfree(buf);
  249. // 检查父目录中是否已经有相同的目录项
  250. if (vfs_path_walk((const char *)path, 0) != NULL)
  251. {
  252. // 目录中已有对应的文件夹
  253. kwarn("Dir '%s' aleardy exists.", path);
  254. return -EEXIST;
  255. }
  256. struct vfs_dir_entry_t *subdir_dentry = vfs_alloc_dentry(pathlen - last_slash);
  257. if (path[pathlen - 1] == '/')
  258. subdir_dentry->name_length = pathlen - last_slash - 2;
  259. else
  260. subdir_dentry->name_length = pathlen - last_slash - 1;
  261. memset((void *)subdir_dentry->name, 0, subdir_dentry->name_length + 1);
  262. for (int i = last_slash + 1, cnt = 0; i < pathlen && cnt < subdir_dentry->name_length; ++i, ++cnt)
  263. {
  264. subdir_dentry->name[cnt] = path[i];
  265. }
  266. ++subdir_dentry->name_length;
  267. // kdebug("last_slash=%d", last_slash);
  268. // kdebug("name=%s", path + last_slash + 1);
  269. subdir_dentry->parent = parent_dir;
  270. // kdebug("to mkdir, parent name=%s", parent_dir->name);
  271. int retval = parent_dir->dir_inode->inode_ops->mkdir(parent_dir->dir_inode, subdir_dentry, 0);
  272. list_append(&parent_dir->subdirs_list, &subdir_dentry->child_node_list);
  273. // kdebug("retval = %d", retval);
  274. return 0;
  275. }
  276. /**
  277. * @brief 创建文件夹
  278. *
  279. * @param path(r8) 路径
  280. * @param mode(r9) 模式
  281. * @return uint64_t
  282. */
  283. uint64_t sys_mkdir(struct pt_regs *regs)
  284. {
  285. const char *path = (const char *)regs->r8;
  286. // kdebug("path = %s", path);
  287. mode_t mode = (mode_t)regs->r9;
  288. if (regs->cs & USER_CS)
  289. return vfs_mkdir(path, mode, true);
  290. else
  291. return vfs_mkdir(path, mode, false);
  292. }
  293. /**
  294. * @brief 打开文件
  295. *
  296. * @param filename 文件路径
  297. * @param flags 标志位
  298. * @return uint64_t 错误码
  299. */
  300. uint64_t do_open(const char *filename, int flags)
  301. {
  302. long path_len = strnlen_user(filename, PAGE_4K_SIZE) + 1;
  303. if (path_len <= 0) // 地址空间错误
  304. {
  305. return -EFAULT;
  306. }
  307. else if (path_len >= PAGE_4K_SIZE) // 名称过长
  308. {
  309. return -ENAMETOOLONG;
  310. }
  311. // 为待拷贝文件路径字符串分配内存空间
  312. char *path = (char *)kzalloc(path_len, 0);
  313. if (path == NULL)
  314. return -ENOMEM;
  315. strncpy_from_user(path, filename, path_len);
  316. // 去除末尾的 '/'
  317. if (path_len >= 2 && path[path_len - 2] == '/')
  318. {
  319. path[path_len - 2] = '\0';
  320. --path_len;
  321. }
  322. // 寻找文件
  323. struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
  324. if (dentry == NULL && flags & O_CREAT)
  325. {
  326. // 先找到倒数第二级目录
  327. int tmp_index = -1;
  328. for (int i = path_len - 1; i >= 0; --i)
  329. {
  330. if (path[i] == '/')
  331. {
  332. tmp_index = i;
  333. break;
  334. }
  335. }
  336. struct vfs_dir_entry_t *parent_dentry = NULL;
  337. // kdebug("tmp_index=%d", tmp_index);
  338. if (tmp_index > 0)
  339. {
  340. path[tmp_index] = '\0';
  341. dentry = vfs_path_walk(path, 0);
  342. if (dentry == NULL)
  343. {
  344. kfree(path);
  345. return -ENOENT;
  346. }
  347. parent_dentry = dentry;
  348. }
  349. else
  350. parent_dentry = vfs_root_sb->root;
  351. // 创建新的文件
  352. dentry = vfs_alloc_dentry(path_len - tmp_index);
  353. dentry->name_length = path_len - tmp_index - 1;
  354. strncpy(dentry->name, path + tmp_index + 1, dentry->name_length);
  355. // kdebug("to create new file:%s namelen=%d", dentry->name, dentry->name_length)
  356. dentry->parent = parent_dentry;
  357. uint64_t retval = parent_dentry->dir_inode->inode_ops->create(parent_dentry->dir_inode, dentry, 0);
  358. if (retval != 0)
  359. {
  360. kfree(dentry->name);
  361. kfree(dentry);
  362. kfree(path);
  363. return retval;
  364. }
  365. list_init(&dentry->child_node_list);
  366. list_init(&dentry->subdirs_list);
  367. list_add(&parent_dentry->subdirs_list, &dentry->child_node_list);
  368. // kdebug("created.");
  369. }
  370. kfree(path);
  371. if (dentry == NULL)
  372. return -ENOENT;
  373. // 要求打开文件夹而目标不是文件夹
  374. if ((flags & O_DIRECTORY) && (dentry->dir_inode->attribute != VFS_IF_DIR))
  375. return -ENOTDIR;
  376. // 创建文件描述符
  377. struct vfs_file_t *file_ptr = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
  378. int errcode = -1;
  379. file_ptr->dEntry = dentry;
  380. file_ptr->mode = flags;
  381. file_ptr->file_ops = dentry->dir_inode->file_ops;
  382. // 如果文件系统实现了打开文件的函数
  383. if (file_ptr->file_ops && file_ptr->file_ops->open)
  384. errcode = file_ptr->file_ops->open(dentry->dir_inode, file_ptr);
  385. if (errcode != VFS_SUCCESS)
  386. {
  387. kfree(file_ptr);
  388. return -EFAULT;
  389. }
  390. if (file_ptr->mode & O_TRUNC) // 清空文件
  391. file_ptr->dEntry->dir_inode->file_size = 0;
  392. if (file_ptr->mode & O_APPEND)
  393. file_ptr->position = file_ptr->dEntry->dir_inode->file_size;
  394. else
  395. file_ptr->position = 0;
  396. struct vfs_file_t **f = current_pcb->fds;
  397. int fd_num = -1;
  398. // 在指针数组中寻找空位
  399. // todo: 当pcb中的指针数组改为动态指针数组之后,需要更改这里(目前还是静态指针数组)
  400. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  401. {
  402. if (f[i] == NULL) // 找到指针数组中的空位
  403. {
  404. fd_num = i;
  405. break;
  406. }
  407. }
  408. // 指针数组没有空位了
  409. if (fd_num == -1)
  410. {
  411. kfree(file_ptr);
  412. return -EMFILE;
  413. }
  414. // 保存文件描述符
  415. f[fd_num] = file_ptr;
  416. return fd_num;
  417. }
  418. uint64_t sys_open(struct pt_regs *regs)
  419. {
  420. char *filename = (char *)(regs->r8);
  421. int flags = (int)(regs->r9);
  422. return do_open(filename, flags);
  423. }
  424. /**
  425. * @brief 动态分配dentry以及路径字符串名称
  426. *
  427. * @param name_size 名称字符串大小(字节)(注意考虑字符串最后需要有一个‘\0’作为结尾)
  428. * @return struct vfs_dir_entry_t* 创建好的dentry
  429. */
  430. struct vfs_dir_entry_t *vfs_alloc_dentry(const int name_size)
  431. {
  432. if (unlikely(name_size > VFS_MAX_PATHLEN))
  433. return NULL;
  434. struct vfs_dir_entry_t *dentry = (struct vfs_dir_entry_t *)kzalloc(sizeof(struct vfs_dir_entry_t), 0);
  435. if (unlikely(dentry == NULL))
  436. return NULL;
  437. if (name_size != 0)
  438. dentry->name = (char *)kzalloc(name_size, 0);
  439. // 初始化lockref
  440. spin_init(&dentry->lockref.lock);
  441. dentry->lockref.count = 1;
  442. // 初始化链表
  443. list_init(&dentry->child_node_list);
  444. list_init(&dentry->subdirs_list);
  445. return dentry;
  446. }
  447. /**
  448. * @brief 判断是否可以删除指定的dentry
  449. *
  450. * 1、我们不能删除一个只读的dentry
  451. * 2、我们应当对这个dentry的inode拥有写、执行权限(暂时还没有实现权限)
  452. * 3、如果dentry指向的是文件夹,而isdir为false,则不能删除
  453. * 3、如果dentry指向的是文件,而isdir为true,则不能删除
  454. * @param dentry 将要被删除的dentry
  455. * @param isdir 是否要删除文件夹
  456. * @return int 错误码
  457. */
  458. int vfs_may_delete(struct vfs_dir_entry_t *dentry, bool isdir)
  459. {
  460. // 当dentry没有inode的时候,认为是bug
  461. BUG_ON(dentry->dir_inode == NULL);
  462. // todo: 进行权限检查
  463. if (isdir) // 要删除文件夹
  464. {
  465. if (!D_ISDIR(dentry))
  466. return -ENOTDIR;
  467. else if (IS_ROOT(dentry))
  468. return -EBUSY;
  469. }
  470. else if (D_ISDIR(dentry)) // 要删除文件但是当前是文件夹
  471. return -EISDIR;
  472. return 0;
  473. }
  474. /**
  475. * @brief 删除文件夹
  476. *
  477. * @param path 文件夹路径
  478. * @param from_userland 请求是否来自用户态
  479. * @return int64_t 错误码
  480. */
  481. int64_t vfs_rmdir(const char *path, bool from_userland)
  482. {
  483. uint32_t pathlen;
  484. if (from_userland)
  485. pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
  486. else
  487. pathlen = strnlen(path, PAGE_4K_SIZE - 1);
  488. if (pathlen == 0)
  489. return -ENOENT;
  490. int last_slash = -1;
  491. // 去除末尾的'/'
  492. for (int i = pathlen - 1; i >= 0; --i)
  493. {
  494. if (path[i] != '/')
  495. {
  496. last_slash = i + 1;
  497. break;
  498. }
  499. }
  500. // 路径格式不合法
  501. if (last_slash < 0)
  502. return -ENOTDIR;
  503. else if (path[0] != '/')
  504. return -EINVAL;
  505. char *buf = (char *)kzalloc(last_slash + 2, 0);
  506. // 拷贝字符串(不包含要被创建的部分)
  507. if (from_userland)
  508. strncpy_from_user(buf, path, last_slash);
  509. else
  510. strncpy(buf, path, last_slash);
  511. buf[last_slash + 1] = '\0';
  512. struct vfs_dir_entry_t *dentry = vfs_path_walk(buf, 0);
  513. if (dentry == NULL)
  514. return -ENOENT;
  515. int retval = vfs_may_delete(dentry, true);
  516. if (retval != 0)
  517. return retval;
  518. // todo: 对dentry和inode加锁
  519. spin_lock(&dentry->lockref.lock);
  520. retval = -EBUSY;
  521. if (is_local_mountpoint(dentry))
  522. goto out;
  523. // todo:
  524. retval = dentry->dir_inode->inode_ops->rmdir(dentry->dir_inode, dentry);
  525. if (retval != 0)
  526. goto out;
  527. dentry->dir_inode->attribute |= VFS_IF_DEAD; // 将当前inode标记为dead
  528. dont_mount(dentry); // 将当前dentry标记为不可被挂载
  529. detach_mounts(dentry); // 清理同样挂载在该路径的所有挂载点的挂载树
  530. if (vfs_dentry_put(dentry) != 0)
  531. goto out; // 释放dentry
  532. return retval;
  533. out:;
  534. spin_unlock(&dentry->lockref.lock);
  535. // todo: 对dentry和inode放锁
  536. return retval;
  537. }
  538. /**
  539. * @brief 删除文件夹的系统调用函数
  540. *
  541. * @param r8 文件夹路径
  542. * @return uint64_t 错误码
  543. */
  544. uint64_t sys_rmdir(struct pt_regs *regs)
  545. {
  546. if (SYSCALL_FROM_USER(regs))
  547. return vfs_rmdir((char *)regs->r8, true);
  548. else
  549. return vfs_rmdir((char *)regs->r8, false);
  550. }
  551. /**
  552. * @brief 分配inode并将引用计数初始化为1
  553. *
  554. * @return struct vfs_index_node_t * 分配得到的inode
  555. */
  556. struct vfs_index_node_t *vfs_alloc_inode()
  557. {
  558. struct vfs_index_node_t *inode = kzalloc(sizeof(struct vfs_index_node_t), 0);
  559. spin_init(&inode->lockref.lock);
  560. inode->lockref.count = 1; // 初始化引用计数为1
  561. return inode;
  562. }
  563. /**
  564. * @brief 初始化vfs
  565. *
  566. * @return int 错误码
  567. */
  568. int vfs_init()
  569. {
  570. mount_init();
  571. rootfs_init();
  572. return 0;
  573. }