DragonOS/kernel/filesystem/VFS/VFS.c

263 lines
7.5 KiB
C
Raw Normal View History

2022-04-21 15:48:47 +00:00
#include "VFS.h"
#include <common/kprint.h>
2022-05-29 06:36:46 +00:00
#include <common/dirent.h>
2022-08-03 07:13:01 +00:00
#include <common/string.h>
2022-05-29 06:36:46 +00:00
#include <common/errno.h>
#include <mm/mm.h>
2022-04-26 16:39:02 +00:00
#include <mm/slab.h>
2022-06-25 12:55:59 +00:00
#include <process/ptrace.h>
#include <process/process.h>
2022-04-21 15:48:47 +00:00
// 为filesystem_type_t结构体实例化一个链表头
static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
/**
* @brief
*
* @param name
* @param DPTE entry
* @param DPT_type
* @param buf
2022-04-21 15:48:47 +00:00
* @return struct vfs_superblock_t*
*/
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)
2022-04-21 15:48:47 +00:00
{
struct vfs_filesystem_type_t *p = NULL;
for (p = &vfs_fs; p; p = p->next)
{
if (!strcmp(p->name, name)) // 存在符合的文件系统
{
return p->read_superblock(DPTE, DPT_type, buf, ahci_ctrl_num, ahci_port_num, part_num);
2022-04-21 15:48:47 +00:00
}
}
kdebug("unsupported fs: %s", name);
return NULL;
}
/**
* @brief VFS中注册文件系统
2022-04-26 16:39:02 +00:00
*
2022-04-21 15:48:47 +00:00
* @param fs
2022-04-26 16:39:02 +00:00
* @return uint64_t
2022-04-21 15:48:47 +00:00
*/
uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs)
{
struct vfs_filesystem_type_t *p = NULL;
2022-04-26 16:39:02 +00:00
for (p = &vfs_fs; p; p = p->next)
2022-04-21 15:48:47 +00:00
{
2022-04-26 16:39:02 +00:00
if (!strcmp(p->name, fs->name)) // 已经注册相同名称的文件系统
2022-04-21 15:48:47 +00:00
return VFS_E_FS_EXISTED;
}
fs->next = vfs_fs.next;
vfs_fs.next = fs;
return VFS_SUCCESS;
}
uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs)
{
struct vfs_filesystem_type_t *p = &vfs_fs;
2022-04-26 16:39:02 +00:00
while (p->next)
2022-04-21 15:48:47 +00:00
{
2022-04-26 16:39:02 +00:00
if (p->next == fs)
2022-04-21 15:48:47 +00:00
{
p->next = p->next->next;
fs->next = NULL;
return VFS_SUCCESS;
}
2022-04-26 16:39:02 +00:00
else
p = p->next;
2022-04-21 15:48:47 +00:00
}
return VFS_E_FS_NOT_EXIST;
2022-04-26 16:39:02 +00:00
}
/**
* @brief
*
* @param path
* @param flags 1 0
* @return struct vfs_dir_entry_t*
*/
struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags)
2022-04-26 16:39:02 +00:00
{
struct vfs_dir_entry_t *parent = vfs_root_sb->root;
// 去除路径前的斜杠
while (*path == '/')
++path;
if ((!*path) || (*path == '\0'))
return parent;
struct vfs_dir_entry_t *dentry;
2022-07-07 02:27:40 +00:00
// kdebug("path before walk:%s", path);
2022-04-26 16:39:02 +00:00
while (true)
{
// 提取出下一级待搜索的目录名或文件名并保存在dEntry_name中
const char *tmp_path = path;
2022-04-26 16:39:02 +00:00
while ((*path && *path != '\0') && (*path != '/'))
++path;
int tmp_path_len = path - tmp_path;
dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
memset(dentry, 0, sizeof(struct vfs_dir_entry_t));
// 为目录项的名称分配内存
dentry->name = (char *)kmalloc(tmp_path_len + 1, 0);
// 貌似这里不需要memset因为空间会被覆盖
// memset(dentry->name, 0, tmp_path_len+1);
2022-07-06 16:55:33 +00:00
memcpy(dentry->name, (void *)tmp_path, tmp_path_len);
2022-04-26 16:39:02 +00:00
dentry->name[tmp_path_len] = '\0';
2022-07-07 02:27:40 +00:00
// kdebug("tmp_path_len=%d, dentry->name= %s", tmp_path_len, dentry->name);
2022-04-26 16:39:02 +00:00
dentry->name_length = tmp_path_len;
if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
{
// 搜索失败
2022-07-07 02:27:40 +00:00
// kerror("cannot find the file/dir : %s", dentry->name);
2022-04-26 16:39:02 +00:00
kfree(dentry->name);
kfree(dentry);
return NULL;
}
// 找到子目录项
// 初始化子目录项的entry
list_init(&dentry->child_node_list);
list_init(&dentry->subdirs_list);
dentry->parent = parent;
list_add(&parent->subdirs_list, &dentry->child_node_list);
2022-04-26 16:39:02 +00:00
while (*path == '/')
++path;
if ((!*path) || (*path == '\0')) // 已经到达末尾
{
if (flags & 1) // 返回父目录
{
return parent;
}
return dentry;
}
parent = dentry;
}
2022-05-27 05:41:10 +00:00
}
/**
* @brief dentry
2022-05-29 06:36:46 +00:00
*
2022-05-27 05:41:10 +00:00
*/
int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
{
2022-05-29 06:36:46 +00:00
struct dirent *dent = (struct dirent *)buf;
// 如果尝试访问内核空间,则返回错误
if (!(verify_area((uint64_t)buf, sizeof(struct dirent) + namelen)))
return -EFAULT;
// ====== 填充dirent结构体 =====
memset(buf, 0, sizeof(struct dirent) + namelen);
memcpy(dent->d_name, name, namelen);
dent->d_name[namelen] = '\0';
// 暂时不显示目录下的记录数
dent->d_reclen = 0;
dent->d_ino = d_ino;
dent->d_off = offset;
dent->d_type = type;
// 返回dirent的总大小
return sizeof(struct dirent) + namelen;
2022-06-25 12:55:59 +00:00
}
/**
* @brief
*
2022-06-25 12:55:59 +00:00
* @param path(r8)
* @param mode(r9)
* @return uint64_t
*/
uint64_t sys_mkdir(struct pt_regs *regs)
{
const char *path = (const char *)regs->r8;
2022-07-07 02:27:40 +00:00
// kdebug("path = %s", path);
2022-06-25 12:55:59 +00:00
mode_t mode = (mode_t)regs->r9;
uint32_t pathlen;
if (regs->cs & USER_CS)
pathlen = strnlen_user(path, PAGE_4K_SIZE - 1);
else
pathlen = strnlen(path, PAGE_4K_SIZE - 1);
if (pathlen == 0)
return -ENOENT;
int last_slash = -1;
// 查找最后一个'/',忽略路径末尾的'/'
for (int i = pathlen - 2; i >= 0; --i)
{
if (path[i] == '/')
{
last_slash = i;
break;
}
}
// 路径格式不合法(必须使用绝对路径)
if (last_slash < 0)
return ENOTDIR;
char *buf = (char *)kmalloc(last_slash + 1, 0);
memset(buf, 0, pathlen + 1);
// 拷贝字符串(不包含要被创建的部分)
if (regs->cs & USER_CS)
strncpy_from_user(buf, path, last_slash);
else
strncpy(buf, path, last_slash);
buf[last_slash + 1] = '\0';
2022-07-07 02:27:40 +00:00
// kdebug("to walk: %s", buf);
2022-06-25 12:55:59 +00:00
// 查找父目录
struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0);
if (parent_dir == NULL)
{
kwarn("parent dir is NULL.");
2022-06-25 12:55:59 +00:00
kfree(buf);
return -ENOENT;
}
kfree(buf);
// 检查父目录中是否已经有相同的目录项
if (vfs_path_walk((const char *)path, 0) != NULL)
2022-06-25 12:55:59 +00:00
{
// 目录中已有对应的文件夹
kwarn("Dir '%s' aleardy exists.", path);
2022-07-07 02:27:40 +00:00
return -EEXIST;
2022-06-25 12:55:59 +00:00
}
struct vfs_dir_entry_t *subdir_dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
memset((void *)subdir_dentry, 0, sizeof(struct vfs_dir_entry_t));
if (path[pathlen - 1] == '/')
subdir_dentry->name_length = pathlen - last_slash - 2;
else
subdir_dentry->name_length = pathlen - last_slash - 1;
subdir_dentry->name = (char *)kmalloc(subdir_dentry->name_length + 1, 0);
memset((void *)subdir_dentry->name, 0, subdir_dentry->name_length + 1);
for (int i = last_slash + 1, cnt = 0; i < pathlen && cnt < subdir_dentry->name_length; ++i, ++cnt)
{
subdir_dentry->name[cnt] = path[i];
}
++subdir_dentry->name_length;
2022-07-07 02:27:40 +00:00
// kdebug("last_slash=%d", last_slash);
// kdebug("name=%s", path + last_slash + 1);
2022-06-25 12:55:59 +00:00
subdir_dentry->parent = parent_dir;
2022-07-07 02:27:40 +00:00
// kdebug("to mkdir, parent name=%s", parent_dir->name);
2022-06-25 12:55:59 +00:00
int retval = parent_dir->dir_inode->inode_ops->mkdir(parent_dir->dir_inode, subdir_dentry, 0);
list_add(&parent_dir->subdirs_list, &subdir_dentry->child_node_list);
2022-07-07 02:27:40 +00:00
// kdebug("retval = %d", retval);
2022-06-25 12:55:59 +00:00
return 0;
2022-04-21 15:48:47 +00:00
}