VFS.c 23 KB

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