VFS.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. // kdebug("tmpname=%s", tmpname);
  146. dentry = vfs_search_dentry_list(parent, tmpname);
  147. kfree(tmpname);
  148. }
  149. // 如果没有找到dentry缓存,则申请新的dentry
  150. if (dentry == NULL)
  151. {
  152. dentry = vfs_alloc_dentry(tmp_path_len + 1);
  153. memcpy(dentry->name, (void *)tmp_path, tmp_path_len);
  154. dentry->name[tmp_path_len] = '\0';
  155. // kdebug("tmp_path_len=%d, dentry->name= %s", tmp_path_len, dentry->name);
  156. dentry->name_length = tmp_path_len;
  157. if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
  158. {
  159. // 搜索失败
  160. // kwarn("cannot find the file/dir : %s", dentry->name);
  161. kfree(dentry->name);
  162. kfree(dentry);
  163. return NULL;
  164. }
  165. // 找到子目录项
  166. dentry->parent = parent;
  167. list_add(&parent->subdirs_list, &dentry->child_node_list);
  168. }
  169. while (*path == '/')
  170. ++path;
  171. if ((!*path) || (*path == '\0')) // 已经到达末尾
  172. {
  173. if (flags & 1) // 返回父目录
  174. {
  175. return parent;
  176. }
  177. return dentry;
  178. }
  179. parent = dentry;
  180. }
  181. }
  182. /**
  183. * @brief 填充dentry
  184. *
  185. * @return dirent的总大小
  186. */
  187. int vfs_fill_dirent(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
  188. {
  189. struct dirent *dent = (struct dirent *)buf;
  190. // 如果尝试访问内核空间,则返回错误
  191. if (!(verify_area((uint64_t)buf, sizeof(struct dirent) + namelen)))
  192. return -EFAULT;
  193. // ====== 填充dirent结构体 =====
  194. memset(buf, 0, sizeof(struct dirent) + namelen);
  195. memcpy(dent->d_name, name, namelen);
  196. dent->d_name[namelen] = '\0';
  197. // 暂时不显示目录下的记录数
  198. dent->d_reclen = 0;
  199. dent->d_ino = d_ino;
  200. dent->d_off = offset;
  201. dent->d_type = type;
  202. // 返回dirent的总大小
  203. return sizeof(struct dirent) + namelen;
  204. }
  205. /**
  206. * @brief 创建文件夹
  207. *
  208. * @param path 文件夹路径
  209. * @param mode 创建模式
  210. * @param from_userland 该创建请求是否来自用户态
  211. * @return int64_t 错误码
  212. */
  213. int64_t vfs_mkdir(const char *path, mode_t mode, bool from_userland)
  214. {
  215. uint32_t pathlen;
  216. int retval = 0;
  217. if (from_userland)
  218. pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
  219. else
  220. pathlen = strnlen(path, PAGE_4K_SIZE - 1);
  221. if (pathlen == 0)
  222. return -ENOENT;
  223. int last_slash = -1;
  224. // 查找最后一个'/',忽略路径末尾的'/'
  225. for (int i = pathlen - 2; i >= 0; --i)
  226. {
  227. if (path[i] == '/')
  228. {
  229. last_slash = i;
  230. break;
  231. }
  232. }
  233. // 路径格式不合法(必须使用绝对路径)
  234. if (last_slash < 0)
  235. return -ENOTDIR;
  236. char *buf = (char *)kzalloc(last_slash + 2, 0);
  237. // 拷贝字符串(不包含要被创建的部分)
  238. if (from_userland)
  239. strncpy_from_user(buf, path, last_slash);
  240. else
  241. strncpy(buf, path, last_slash);
  242. buf[last_slash + 1] = '\0';
  243. // 查找父目录
  244. struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0);
  245. if (parent_dir == NULL)
  246. {
  247. kwarn("parent dir is NULL.");
  248. kfree(buf);
  249. return -ENOENT;
  250. }
  251. kfree(buf);
  252. // 检查父目录中是否已经有相同的目录项
  253. if (vfs_path_walk((const char *)path, 0) != NULL)
  254. {
  255. // 目录中已有对应的文件夹
  256. kwarn("Dir '%s' aleardy exists.", path);
  257. return -EEXIST;
  258. }
  259. spin_lock(&parent_dir->lockref.lock);
  260. struct vfs_dir_entry_t *subdir_dentry = vfs_alloc_dentry(pathlen - last_slash);
  261. if (path[pathlen - 1] == '/')
  262. subdir_dentry->name_length = pathlen - last_slash - 2;
  263. else
  264. subdir_dentry->name_length = pathlen - last_slash - 1;
  265. for (int i = last_slash + 1, cnt = 0; i < pathlen && cnt < subdir_dentry->name_length; ++i, ++cnt)
  266. subdir_dentry->name[cnt] = path[i];
  267. // 设置subdir的dentry的父路径
  268. subdir_dentry->parent = parent_dir;
  269. // kdebug("to mkdir, parent name=%s", parent_dir->name);
  270. spin_lock(&parent_dir->dir_inode->lockref.lock);
  271. retval = parent_dir->dir_inode->inode_ops->mkdir(parent_dir->dir_inode, subdir_dentry, 0);
  272. spin_unlock(&parent_dir->dir_inode->lockref.lock);
  273. if (retval != 0)
  274. {
  275. if (vfs_dentry_put(parent_dir) != 0) // 释放dentry
  276. spin_unlock(&parent_dir->lockref.lock);
  277. return retval;
  278. }
  279. // 获取append前一个dentry并加锁
  280. struct List *target_list = &parent_dir->subdirs_list;
  281. // kdebug("target_list=%#018lx target_list->prev=%#018lx",target_list,target_list->prev);
  282. if (list_empty(target_list) == false)
  283. {
  284. struct vfs_dir_entry_t *prev_dentry = list_entry(target_list->prev, struct vfs_dir_entry_t, child_node_list);
  285. // kdebug("prev_dentry%#018lx",prev_dentry);
  286. spin_lock(&prev_dentry->lockref.lock);
  287. list_append(&parent_dir->subdirs_list, &subdir_dentry->child_node_list);
  288. // kdebug("retval = %d", retval);
  289. spin_unlock(&prev_dentry->lockref.lock);
  290. }
  291. else
  292. {
  293. list_append(&parent_dir->subdirs_list, &subdir_dentry->child_node_list);
  294. goto out;
  295. }
  296. out:;
  297. spin_unlock(&parent_dir->lockref.lock);
  298. return retval;
  299. }
  300. /**
  301. * @brief 创建文件夹
  302. *
  303. * @param path(r8) 路径
  304. * @param mode(r9) 模式
  305. * @return uint64_t
  306. */
  307. uint64_t sys_mkdir(struct pt_regs *regs)
  308. {
  309. const char *path = (const char *)regs->r8;
  310. // kdebug("path = %s", path);
  311. mode_t mode = (mode_t)regs->r9;
  312. if (regs->cs & USER_CS)
  313. return vfs_mkdir(path, mode, true);
  314. else
  315. return vfs_mkdir(path, mode, false);
  316. }
  317. /**
  318. * @brief 打开文件
  319. *
  320. * @param filename 文件路径
  321. * @param flags 标志位
  322. * @return uint64_t 错误码
  323. */
  324. uint64_t do_open(const char *filename, int flags)
  325. {
  326. long path_len = strnlen_user(filename, PAGE_4K_SIZE) + 1;
  327. if (path_len <= 0) // 地址空间错误
  328. return -EFAULT;
  329. else if (path_len >= PAGE_4K_SIZE) // 名称过长
  330. return -ENAMETOOLONG;
  331. // 为待拷贝文件路径字符串分配内存空间
  332. char *path = (char *)kzalloc(path_len, 0);
  333. if (path == NULL)
  334. return -ENOMEM;
  335. strncpy_from_user(path, filename, path_len);
  336. // 去除末尾的 '/'
  337. if (path_len >= 2 && path[path_len - 2] == '/')
  338. {
  339. path[path_len - 2] = '\0';
  340. --path_len;
  341. }
  342. // 寻找文件
  343. struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
  344. if (dentry == NULL && flags & O_CREAT)
  345. {
  346. // 先找到倒数第二级目录
  347. int tmp_index = -1;
  348. for (int i = path_len - 1; i >= 0; --i)
  349. {
  350. if (path[i] == '/')
  351. {
  352. tmp_index = i;
  353. break;
  354. }
  355. }
  356. struct vfs_dir_entry_t *parent_dentry = NULL;
  357. // kdebug("tmp_index=%d", tmp_index);
  358. if (tmp_index > 0)
  359. {
  360. path[tmp_index] = '\0';
  361. parent_dentry = vfs_path_walk(path, 0);
  362. if (parent_dentry == NULL)
  363. {
  364. kfree(path);
  365. return -ENOENT;
  366. }
  367. }
  368. else
  369. {
  370. parent_dentry = vfs_root_sb->root;
  371. }
  372. // 创建新的文件
  373. dentry = vfs_alloc_dentry(path_len - tmp_index);
  374. dentry->name_length = path_len - tmp_index - 2;
  375. // kdebug("to create new file:%s namelen=%d", dentry->name, dentry->name_length);
  376. strncpy(dentry->name, path + tmp_index + 1, dentry->name_length);
  377. dentry->parent = parent_dentry;
  378. // 对父目录项加锁
  379. spin_lock(&parent_dentry->lockref.lock);
  380. spin_lock(&parent_dentry->dir_inode->lockref.lock);
  381. // 创建子目录项
  382. uint64_t retval = parent_dentry->dir_inode->inode_ops->create(parent_dentry->dir_inode, dentry, 0);
  383. spin_unlock(&parent_dentry->dir_inode->lockref.lock); // 解锁inode
  384. if (retval != 0)
  385. {
  386. if (vfs_dentry_put(dentry) != 0) // 释放dentry
  387. BUG_ON(1);
  388. BUG_ON(1);
  389. kfree(path);
  390. spin_unlock(&parent_dentry->lockref.lock);
  391. return retval;
  392. }
  393. // ==== 将子目录项添加到链表 ====
  394. struct vfs_dir_entry_t *next_dentry = NULL;
  395. // 若list非空,则对前一个dentry加锁
  396. if (!list_empty(&parent_dentry->subdirs_list))
  397. {
  398. next_dentry = list_entry(list_next(&parent_dentry->subdirs_list), struct vfs_dir_entry_t, child_node_list);
  399. spin_lock(&next_dentry->lockref.lock);
  400. }
  401. list_add(&parent_dentry->subdirs_list, &dentry->child_node_list);
  402. if (next_dentry != NULL)
  403. spin_unlock(&next_dentry->lockref.lock);
  404. // 新建文件结束,对父目录项解锁
  405. spin_unlock(&parent_dentry->lockref.lock);
  406. // kdebug("created.");
  407. }
  408. kfree(path);
  409. if (dentry == NULL)
  410. {
  411. return -ENOENT;
  412. }
  413. spin_lock(&dentry->lockref.lock);
  414. // 要求打开文件夹而目标不是文件夹
  415. if ((flags & O_DIRECTORY) && (dentry->dir_inode->attribute != VFS_IF_DIR))
  416. {
  417. spin_unlock(&dentry->lockref.lock);
  418. return -ENOTDIR;
  419. }
  420. // 创建文件描述符
  421. struct vfs_file_t *file_ptr = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
  422. int errcode = -1;
  423. file_ptr->dEntry = dentry;
  424. file_ptr->mode = flags;
  425. file_ptr->file_ops = dentry->dir_inode->file_ops;
  426. // 如果文件系统实现了打开文件的函数
  427. if (file_ptr->file_ops && file_ptr->file_ops->open)
  428. errcode = file_ptr->file_ops->open(dentry->dir_inode, file_ptr);
  429. if (errcode != 0)
  430. {
  431. kfree(file_ptr);
  432. spin_unlock(&dentry->lockref.lock);
  433. return -EFAULT;
  434. }
  435. if (file_ptr->mode & O_TRUNC) // 清空文件
  436. file_ptr->dEntry->dir_inode->file_size = 0;
  437. if (file_ptr->mode & O_APPEND)
  438. file_ptr->position = file_ptr->dEntry->dir_inode->file_size;
  439. else
  440. file_ptr->position = 0;
  441. int fd_num = process_fd_alloc(file_ptr);
  442. // 指针数组没有空位了
  443. if (fd_num == -1)
  444. {
  445. kfree(file_ptr);
  446. spin_unlock(&dentry->lockref.lock);
  447. return -ENFILE;
  448. }
  449. spin_unlock(&dentry->lockref.lock);
  450. return fd_num;
  451. }
  452. uint64_t sys_open(struct pt_regs *regs)
  453. {
  454. char *filename = (char *)(regs->r8);
  455. int flags = (int)(regs->r9);
  456. return do_open(filename, flags);
  457. }
  458. /**
  459. * @brief 动态分配dentry以及路径字符串名称
  460. *
  461. * @param name_size 名称字符串大小(字节)(注意考虑字符串最后需要有一个‘\0’作为结尾)
  462. * @return struct vfs_dir_entry_t* 创建好的dentry
  463. */
  464. struct vfs_dir_entry_t *vfs_alloc_dentry(const int name_size)
  465. {
  466. if (unlikely(name_size > VFS_MAX_PATHLEN))
  467. return NULL;
  468. struct vfs_dir_entry_t *dentry = (struct vfs_dir_entry_t *)kzalloc(sizeof(struct vfs_dir_entry_t), 0);
  469. if (unlikely(dentry == NULL))
  470. return NULL;
  471. if (name_size != 0)
  472. dentry->name = (char *)kzalloc(name_size, 0);
  473. // 初始化lockref
  474. spin_init(&dentry->lockref.lock);
  475. dentry->lockref.count = 1;
  476. // 初始化链表
  477. list_init(&dentry->child_node_list);
  478. list_init(&dentry->subdirs_list);
  479. return dentry;
  480. }
  481. /**
  482. * @brief 判断是否可以删除指定的dentry
  483. *
  484. * 1、我们不能删除一个只读的dentry
  485. * 2、我们应当对这个dentry的inode拥有写、执行权限(暂时还没有实现权限)
  486. * 3、如果dentry指向的是文件夹,而isdir为false,则不能删除
  487. * 3、如果dentry指向的是文件,而isdir为true,则不能删除
  488. * @param dentry 将要被删除的dentry
  489. * @param isdir 是否要删除文件夹
  490. * @return int 错误码
  491. */
  492. int vfs_may_delete(struct vfs_dir_entry_t *dentry, bool isdir)
  493. {
  494. // 当dentry没有inode的时候,认为是bug
  495. BUG_ON(dentry->dir_inode == NULL);
  496. // todo: 进行权限检查
  497. if (isdir) // 要删除文件夹
  498. {
  499. if (!D_ISDIR(dentry))
  500. return -ENOTDIR;
  501. else if (IS_ROOT(dentry))
  502. return -EBUSY;
  503. }
  504. else if (D_ISDIR(dentry)) // 要删除文件但是当前是文件夹
  505. return -EISDIR;
  506. return 0;
  507. }
  508. /**
  509. * @brief 删除文件夹
  510. *
  511. * @param path 文件夹路径
  512. * @param from_userland 请求是否来自用户态
  513. * @return int64_t 错误码
  514. */
  515. int64_t vfs_rmdir(const char *path, bool from_userland)
  516. {
  517. uint32_t pathlen;
  518. int retval = 0;
  519. if (from_userland)
  520. pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
  521. else
  522. pathlen = strnlen(path, PAGE_4K_SIZE - 1);
  523. if (pathlen == 0)
  524. return -ENOENT;
  525. int last_slash = -1;
  526. // 去除末尾的'/'
  527. for (int i = pathlen - 1; i >= 0; --i)
  528. {
  529. if (path[i] != '/')
  530. {
  531. last_slash = i + 1;
  532. break;
  533. }
  534. }
  535. // 路径格式不合法
  536. if (last_slash < 0)
  537. return -ENOTDIR;
  538. else if (path[0] != '/')
  539. return -EINVAL;
  540. char *buf = (char *)kzalloc(last_slash + 2, 0);
  541. // 拷贝字符串(不包含要被创建的部分)
  542. if (from_userland)
  543. strncpy_from_user(buf, path, last_slash);
  544. else
  545. strncpy(buf, path, last_slash);
  546. buf[last_slash + 1] = '\0';
  547. struct vfs_dir_entry_t *dentry = vfs_path_walk(buf, 0);
  548. kfree(buf);
  549. if (dentry == NULL)
  550. {
  551. retval = -ENOENT;
  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 + 1, 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] = '\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->lockref.lock);
  686. spin_lock(&dentry->lockref.lock);
  687. retval = vfs_unlink(NULL, dentry->parent->dir_inode, dentry, NULL);
  688. if (unlikely(retval != 0))
  689. {
  690. // kdebug("retval=%d", retval);
  691. spin_unlock(&dentry->lockref.lock);
  692. spin_unlock(&p_inode->lockref.lock);
  693. goto out;
  694. }
  695. // kdebug("vfs_dentry_put=%d", retval);
  696. spin_unlock(&dentry->lockref.lock);
  697. spin_unlock(&p_inode->lockref.lock);
  698. if (IS_ERR_VALUE(retval))
  699. kwarn("In do_unlink_at: dentry put failed; retval=%d", retval);
  700. else
  701. retval = 0;
  702. out:;
  703. return retval;
  704. }
  705. /**
  706. * @brief 删除文件夹、取消文件的链接、删除文件的系统调用
  707. *
  708. * @param regs->r8 dfd 进程相对路径基准目录的文件描述符(见fcntl.h)
  709. * @param regs->r9 路径名称字符串
  710. * @param regs->r10 flag 预留的标志位,暂时未使用,请置为0。
  711. * @return uint64_t 错误码
  712. */
  713. uint64_t sys_unlink_at(struct pt_regs *regs)
  714. {
  715. int dfd = regs->r8;
  716. const char *pathname = (const char *)regs->r9;
  717. int flag = regs->r10;
  718. bool from_user = SYSCALL_FROM_USER(regs) ? true : false;
  719. if ((flag & (~AT_REMOVEDIR)) != 0)
  720. return -EINVAL;
  721. if (flag & AT_REMOVEDIR)
  722. return vfs_rmdir(pathname, from_user);
  723. // kdebug("to do_unlink_at, path=%s", pathname);
  724. return do_unlink_at(dfd, pathname, from_user);
  725. }
  726. /**
  727. * @brief 分配inode并将引用计数初始化为1
  728. *
  729. * @return struct vfs_index_node_t * 分配得到的inode
  730. */
  731. struct vfs_index_node_t *vfs_alloc_inode()
  732. {
  733. struct vfs_index_node_t *inode = kzalloc(sizeof(struct vfs_index_node_t), 0);
  734. spin_init(&inode->lockref.lock);
  735. inode->lockref.count = 1; // 初始化引用计数为1
  736. return inode;
  737. }
  738. /**
  739. * @brief 初始化vfs
  740. *
  741. * @return int 错误码
  742. */
  743. int vfs_init()
  744. {
  745. mount_init();
  746. rootfs_init();
  747. return 0;
  748. }