mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-19 17:26:31 +00:00
🆕 fat32文件系统的mkdir
This commit is contained in:
@ -3,10 +3,13 @@ CFLAGS += -I .
|
||||
|
||||
kernel_common_subdirs:=libELF
|
||||
|
||||
all:
|
||||
all: glib.o
|
||||
@list='$(kernel_common_subdirs)'; for subdir in $$list; do \
|
||||
echo "make all in $$subdir";\
|
||||
cd $$subdir;\
|
||||
$(MAKE) all CFLAGS="$(CFLAGS)";\
|
||||
cd ..;\
|
||||
done
|
||||
|
||||
glib.o: glib.c
|
||||
gcc $(CFLAGS) -c glib.c -o glib.o
|
31
kernel/common/glib.c
Normal file
31
kernel/common/glib.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include "glib.h"
|
||||
|
||||
/**
|
||||
* @brief 测量来自用户空间的字符串的长度,会检验地址空间是否属于用户空间
|
||||
* @param src
|
||||
* @param maxlen
|
||||
* @return long
|
||||
*/
|
||||
long strnlen_user(const char *src, unsigned long maxlen)
|
||||
{
|
||||
|
||||
unsigned long size = strlen(src);
|
||||
// 地址不合法
|
||||
if (!verify_area((uint64_t)src, size))
|
||||
return 0;
|
||||
|
||||
return size <= maxlen ? size : maxlen;
|
||||
}
|
||||
|
||||
long strnlen(const char *src, unsigned long maxlen)
|
||||
{
|
||||
|
||||
if (src == NULL)
|
||||
return 0;
|
||||
register int __res = 0;
|
||||
while (src[__res] != '\0' && __res < maxlen)
|
||||
{
|
||||
++__res;
|
||||
}
|
||||
return __res;
|
||||
}
|
@ -168,7 +168,7 @@ static inline struct List *list_next(struct List *entry)
|
||||
}
|
||||
|
||||
//计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
|
||||
static inline int strlen(char *s)
|
||||
static inline int strlen(const char *s)
|
||||
{
|
||||
if(s == NULL)
|
||||
return 0;
|
||||
@ -180,6 +180,15 @@ static inline int strlen(char *s)
|
||||
return __res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 测量字符串的长度
|
||||
*
|
||||
* @param src 字符串
|
||||
* @param maxlen 最大长度
|
||||
* @return long
|
||||
*/
|
||||
long strnlen(const char *src, unsigned long maxlen);
|
||||
|
||||
void *memset(void *dst, unsigned char C, ul size)
|
||||
{
|
||||
|
||||
@ -530,18 +539,9 @@ static inline uint64_t copy_to_user(void *dst, void *src, uint64_t size)
|
||||
* @param maxlen
|
||||
* @return long
|
||||
*/
|
||||
long strnlen_user(void *src, unsigned long maxlen)
|
||||
{
|
||||
long strnlen_user(const char *src, unsigned long maxlen);
|
||||
|
||||
unsigned long size = strlen(src);
|
||||
// 地址不合法
|
||||
if (!verify_area((uint64_t)src, size))
|
||||
return 0;
|
||||
|
||||
return size <= maxlen ? size : maxlen;
|
||||
}
|
||||
|
||||
char *strncpy(char *Dest, char *Src, long Count)
|
||||
char *strncpy(char *Dest, const char *Src, long Count)
|
||||
{
|
||||
__asm__ __volatile__("cld \n\t"
|
||||
"1: \n\t"
|
||||
@ -560,7 +560,7 @@ char *strncpy(char *Dest, char *Src, long Count)
|
||||
return Dest;
|
||||
}
|
||||
|
||||
long strncpy_from_user(void *dst, void *src, unsigned long size)
|
||||
long strncpy_from_user(char *dst, const char *src, unsigned long size)
|
||||
{
|
||||
if (!verify_area((uint64_t)src, size))
|
||||
return 0;
|
||||
|
@ -186,6 +186,7 @@ void do_general_protection(struct pt_regs *regs, unsigned long error_code)
|
||||
printk_color(RED, BLACK, "Refers to a descriptor in the current GDT;\n");
|
||||
|
||||
printk_color(RED, BLACK, "Segment Selector Index:%#010x\n", error_code & 0xfff8);
|
||||
traceback(regs);
|
||||
while (1)
|
||||
hlt();
|
||||
}
|
||||
@ -224,6 +225,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long error_code)
|
||||
|
||||
printk_color(RED, BLACK, "CR2:%#018lx\n", cr2);
|
||||
|
||||
traceback(regs);
|
||||
current_pcb->state = PROC_STOPPED;
|
||||
while (1)
|
||||
hlt();
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <common/errno.h>
|
||||
#include <mm/mm.h>
|
||||
#include <mm/slab.h>
|
||||
#include <process/ptrace.h>
|
||||
#include <process/process.h>
|
||||
|
||||
// 为filesystem_type_t结构体实例化一个链表头
|
||||
static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
|
||||
@ -165,3 +167,93 @@ int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned ch
|
||||
// 返回dirent的总大小
|
||||
return sizeof(struct dirent) + namelen;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 创建文件夹
|
||||
*
|
||||
* @param path(r8) 路径
|
||||
* @param mode(r9) 模式
|
||||
* @return uint64_t
|
||||
*/
|
||||
uint64_t sys_mkdir(struct pt_regs *regs)
|
||||
{
|
||||
const char *path = (const char *)regs->r8;
|
||||
kdebug("path = %s", path);
|
||||
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';
|
||||
// 查找父目录
|
||||
struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0);
|
||||
|
||||
if (parent_dir == NULL)
|
||||
{
|
||||
kfree(buf);
|
||||
return -ENOENT;
|
||||
}
|
||||
kfree(buf);
|
||||
|
||||
// 检查父目录中是否已经有相同的目录项
|
||||
if (vfs_path_walk(path, 0) != NULL)
|
||||
{
|
||||
// 目录中已有对应的文件夹
|
||||
kwarn("Dir '%s' aleardy exists.", path);
|
||||
return -EEXIST;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
kdebug("last_slash=%d", last_slash);
|
||||
kdebug("name=%s", path + last_slash + 1);
|
||||
subdir_dentry->parent = parent_dir;
|
||||
kdebug("to mkdir, parent name=%s", parent_dir->name);
|
||||
int retval = parent_dir->dir_inode->inode_ops->mkdir(parent_dir->dir_inode, subdir_dentry, 0);
|
||||
kdebug("retval = %d", retval);
|
||||
return 0;
|
||||
}
|
@ -108,7 +108,19 @@ struct vfs_super_block_operations_t
|
||||
struct vfs_inode_operations_t
|
||||
{
|
||||
long (*create)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
|
||||
/**
|
||||
* @brief 在文件系统中查找指定的目录项
|
||||
* @param parent_inode 父目录项(在这个目录下查找)
|
||||
* @param dest_dEntry 构造的目标目录项的结构体(传入名称,然后更多的详细信息将在本函数中完成填写)
|
||||
*
|
||||
*/
|
||||
struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry);
|
||||
/**
|
||||
* @brief 创建文件夹
|
||||
* @param inode 父目录的inode
|
||||
* @param dEntry 新的文件夹的dentry
|
||||
* @param mode 创建文件夹的mode
|
||||
*/
|
||||
long (*mkdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode);
|
||||
long (*rmdir)(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry);
|
||||
long (*rename)(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry);
|
||||
|
@ -38,6 +38,22 @@ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t
|
||||
return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算短目录项文件名的校验和
|
||||
*
|
||||
* @param name 短目录项文件名字符串(长度为11)
|
||||
* @return uint8_t 校验和
|
||||
*/
|
||||
static uint8_t fat32_ChkSum(uint8_t *name)
|
||||
{
|
||||
uint8_t chksum = 0;
|
||||
for (uint8_t i = 0; i < 11; ++i)
|
||||
{
|
||||
chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + *name;
|
||||
++name;
|
||||
}
|
||||
return chksum;
|
||||
}
|
||||
/**
|
||||
* @brief 读取指定簇的FAT表项
|
||||
*
|
||||
@ -75,19 +91,19 @@ uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t
|
||||
// 计算每个扇区内含有的FAT表项数
|
||||
// FAT每项4bytes
|
||||
uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
|
||||
uint32_t buf[256];
|
||||
uint32_t *buf = kmalloc(fsbi->bytes_per_sec, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_sec);
|
||||
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
|
||||
buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
|
||||
// 向FAT1和FAT2写入数据
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
kfree(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -95,7 +111,7 @@ uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t
|
||||
* @brief 在父目录中寻找指定的目录项
|
||||
*
|
||||
* @param parent_inode 父目录项的inode
|
||||
* @param dest_inode 搜索目标目录项的inode
|
||||
* @param dest_dentry 搜索目标目录项
|
||||
* @return struct vfs_dir_entry_t* 目标目录项
|
||||
*/
|
||||
struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dentry)
|
||||
@ -134,10 +150,10 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru
|
||||
if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
|
||||
continue;
|
||||
|
||||
// 跳过无效页表项、空闲页表项
|
||||
// 跳过无效目录项、空闲目录项
|
||||
if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
|
||||
continue;
|
||||
|
||||
// kdebug("short name [%d] %s\n 33333==[%#02x]", i / 32, tmp_dEntry->DIR_Name, tmp_dEntry->DIR_Name[3]);
|
||||
// 找到长目录项,位于短目录项之前
|
||||
struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
|
||||
|
||||
@ -184,6 +200,7 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru
|
||||
js = 0;
|
||||
for (int x = 0; x < 8; ++x)
|
||||
{
|
||||
// kdebug("value = %#02x", tmp_dEntry->DIR_Name[x]);
|
||||
switch (tmp_dEntry->DIR_Name[x])
|
||||
{
|
||||
case ' ':
|
||||
@ -349,6 +366,10 @@ find_lookup_success:; // 找到目标dentry
|
||||
p->attribute |= VFS_ATTR_DEVICE;
|
||||
|
||||
dest_dentry->dir_inode = p;
|
||||
dest_dentry->dir_ops = &fat32_dEntry_ops;
|
||||
list_init(&dest_dentry->child_node_list);
|
||||
list_init(&dest_dentry->subdirs_list);
|
||||
|
||||
kfree(buf);
|
||||
return dest_dentry;
|
||||
}
|
||||
@ -667,9 +688,13 @@ uint64_t fat32_find_available_cluster(fat32_sb_info_t *fsbi)
|
||||
{
|
||||
// 找到空闲簇
|
||||
if ((buf[j] & 0x0fffffff) == 0)
|
||||
{
|
||||
kfree(buf);
|
||||
return i * ent_per_sec + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
kfree(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -876,9 +901,262 @@ long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry
|
||||
{
|
||||
}
|
||||
|
||||
// todo: mkdir
|
||||
int64_t fat32_mkdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode)
|
||||
/**
|
||||
* @brief 在父亲inode的目录项簇中,寻找连续num个空的目录项
|
||||
*
|
||||
* @param parent_inode 父inode
|
||||
* @param num 请求的目录项数量
|
||||
* @param mode 操作模式
|
||||
* @param res_sector 返回信息:缓冲区对应的扇区号
|
||||
* @param res_cluster 返回信息:缓冲区对应的簇号
|
||||
* @param res_data_buf_base 返回信息:缓冲区的内存基地址(记得要释放缓冲区内存)
|
||||
* @return struct fat32_Directory_t* 符合要求的entry的指针(指向地址高处的空目录项,也就是说,有连续num个≤这个指针的空目录项)
|
||||
*/
|
||||
struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *parent_inode, uint32_t num, uint32_t mode, uint32_t *res_sector, uint64_t *res_cluster, uint64_t *res_data_buf_base)
|
||||
{
|
||||
kdebug("find empty_dentry");
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
|
||||
|
||||
uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_clus);
|
||||
|
||||
// 计算父目录项的起始簇号
|
||||
uint32_t cluster = finode->first_clus;
|
||||
|
||||
struct fat32_Directory_t *tmp_dEntry = NULL;
|
||||
// 指向最终的有用的dentry的指针
|
||||
struct fat32_Directory_t *result_dEntry = NULL;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// 计算父目录项的起始LBA扇区号
|
||||
uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
|
||||
// 读取父目录项的起始簇数据
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
tmp_dEntry = (struct fat32_Directory_t *)buf;
|
||||
// 计数连续的空目录项
|
||||
uint32_t count_continuity = 0;
|
||||
|
||||
// 查找连续num个空闲目录项
|
||||
for (int i = 0; (i < fsbi->bytes_per_clus) && count_continuity < num; i += 32, ++tmp_dEntry)
|
||||
{
|
||||
if (!(tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00))
|
||||
{
|
||||
count_continuity = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (count_continuity == 0)
|
||||
result_dEntry = tmp_dEntry;
|
||||
++count_continuity;
|
||||
}
|
||||
|
||||
// 成功查找到符合要求的目录项
|
||||
if (count_continuity == num)
|
||||
{
|
||||
result_dEntry += (num - 1);
|
||||
*res_sector = sector;
|
||||
*res_data_buf_base = (uint64_t)buf;
|
||||
*res_cluster = cluster;
|
||||
return result_dEntry;
|
||||
}
|
||||
|
||||
// 当前簇没有发现符合条件的空闲目录项,寻找下一个簇
|
||||
uint old_cluster = cluster;
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到符合要求的空目录项
|
||||
{
|
||||
// 新增一个簇
|
||||
cluster = fat32_find_available_cluster(fsbi);
|
||||
kdebug("try to allocate a new cluster to parent dentry, cluster=%d, old_cluster=%d", cluster, old_cluster);
|
||||
if (cluster == 0)
|
||||
{
|
||||
kerror("Cannot allocate a new cluster!");
|
||||
while (1)
|
||||
pause();
|
||||
}
|
||||
fat32_write_FAT_entry(fsbi, old_cluster, cluster);
|
||||
fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8);
|
||||
|
||||
// 将这个新的簇清空
|
||||
sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
void *tmp_buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(tmp_buf, 0, fsbi->bytes_per_clus);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
kfree(tmp_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief 创建文件夹
|
||||
* @param inode 父目录的inode
|
||||
* @param dEntry 新的文件夹的dentry
|
||||
* @param mode 创建文件夹的mode
|
||||
*/
|
||||
int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dEntry, int mode)
|
||||
{
|
||||
|
||||
// 先检查是否有重名的目录项,然后分配一个簇
|
||||
|
||||
// 文件系统超级块信息
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
|
||||
// 父目录项的inode
|
||||
struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
|
||||
// ======== todo:检验名称的合法性
|
||||
|
||||
// ====== 找一块连续的区域放置新的目录项 =====
|
||||
|
||||
// 计算总共需要多少个目录项
|
||||
uint32_t cnt_longname = (dEntry->name_length + 25) / 26;
|
||||
if (cnt_longname == 0)
|
||||
cnt_longname = 1;
|
||||
|
||||
// 空闲dentry所在的扇区号
|
||||
uint32_t tmp_dentry_sector = 0;
|
||||
// 空闲dentry所在的缓冲区的基地址
|
||||
uint64_t tmp_dentry_clus_buf_addr = 0;
|
||||
uint64_t tmp_parent_dentry_clus = 0;
|
||||
// 寻找空闲目录项
|
||||
struct fat32_Directory_t *empty_fat32_dentry = fat32_find_empty_dentry(parent_inode, cnt_longname + 1, 0, &tmp_dentry_sector, &tmp_parent_dentry_clus, &tmp_dentry_clus_buf_addr);
|
||||
kdebug("found empty dentry");
|
||||
// ====== 为新的文件夹分配一个簇 =======
|
||||
uint32_t new_dir_clus = fat32_find_available_cluster(fsbi);
|
||||
kdebug("new_dir_clus=%d", new_dir_clus);
|
||||
fat32_write_FAT_entry(fsbi, new_dir_clus, 0x0ffffff8);
|
||||
|
||||
// ====== 填写短目录项
|
||||
memset(empty_fat32_dentry, 0, sizeof(struct fat32_Directory_t));
|
||||
{
|
||||
int tmp_index = 0;
|
||||
// kdebug("dEntry->name_length=%d", dEntry->name_length);
|
||||
for (tmp_index = 0; tmp_index < min(8, dEntry->name_length); ++tmp_index)
|
||||
{
|
||||
if (dEntry->name[tmp_index] == '.')
|
||||
break;
|
||||
empty_fat32_dentry->DIR_Name[tmp_index] = dEntry->name[tmp_index];
|
||||
}
|
||||
|
||||
// 不满的部分使用0x20填充
|
||||
while (tmp_index < 11)
|
||||
{
|
||||
// kdebug("tmp index = %d", tmp_index);
|
||||
dEntry->name[tmp_index] = 0x20;
|
||||
++tmp_index;
|
||||
}
|
||||
}
|
||||
|
||||
empty_fat32_dentry->DIR_Attr = ATTR_DIRECTORY;
|
||||
empty_fat32_dentry->DIR_FileSize = fsbi->bytes_per_clus;
|
||||
empty_fat32_dentry->DIR_FstClusHI = (uint16_t)((new_dir_clus >> 16) & 0x0fff);
|
||||
empty_fat32_dentry->DIR_FstClusLO = (uint16_t)(new_dir_clus & 0xffff);
|
||||
|
||||
// 计算校验和
|
||||
uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
|
||||
// todo: 填写短目录项中的时间信息
|
||||
|
||||
// ======== 填写长目录项
|
||||
uint32_t current_name_index = 0;
|
||||
struct fat32_LongDirectory_t *Ldentry = (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1);
|
||||
for (int i = 1; i <= cnt_longname; ++i, --Ldentry)
|
||||
{
|
||||
Ldentry->LDIR_Ord = i;
|
||||
|
||||
for (int j = 0; j < 5; ++j, ++current_name_index)
|
||||
{
|
||||
if (current_name_index < dEntry->name_length)
|
||||
Ldentry->LDIR_Name1[j] = dEntry->name[current_name_index];
|
||||
else
|
||||
Ldentry->LDIR_Name1[j] = 0xffff;
|
||||
}
|
||||
for (int j = 0; j < 6; ++j, ++current_name_index)
|
||||
{
|
||||
if (current_name_index < dEntry->name_length)
|
||||
Ldentry->LDIR_Name2[j] = dEntry->name[current_name_index];
|
||||
else
|
||||
Ldentry->LDIR_Name2[j] = 0xffff;
|
||||
}
|
||||
for (int j = 0; j < 2; ++j, ++current_name_index)
|
||||
{
|
||||
if (current_name_index < dEntry->name_length)
|
||||
Ldentry->LDIR_Name3[j] = dEntry->name[current_name_index];
|
||||
else
|
||||
Ldentry->LDIR_Name3[j] = 0xffff;
|
||||
}
|
||||
Ldentry->LDIR_Attr = ATTR_LONG_NAME;
|
||||
Ldentry->LDIR_FstClusLO = 0;
|
||||
Ldentry->LDIR_Type = 0;
|
||||
Ldentry->LDIR_Chksum = short_dentry_ChkSum;
|
||||
}
|
||||
// 最后一个长目录项的ord要|=0x40
|
||||
Ldentry->LDIR_Ord = 0xe5 | 0x40;
|
||||
|
||||
// ====== 将目录项写回磁盘
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
// ====== 初始化新的文件夹的目录项 =====
|
||||
{
|
||||
void *buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
struct fat32_Directory_t *new_dir_dentries = (struct fat32_Directory_t *)buf;
|
||||
memset((void *)new_dir_dentries, 0, fsbi->bytes_per_clus);
|
||||
|
||||
// 新增 . 目录项
|
||||
new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
|
||||
new_dir_dentries->DIR_FileSize = 0;
|
||||
new_dir_dentries->DIR_Name[0] = '.';
|
||||
for (int i = 1; i < 11; ++i)
|
||||
new_dir_dentries->DIR_Name[i] = 0x20;
|
||||
|
||||
new_dir_dentries->DIR_FstClusHI = empty_fat32_dentry->DIR_FstClusHI;
|
||||
new_dir_dentries->DIR_FstClusLO = empty_fat32_dentry->DIR_FstClusLO;
|
||||
|
||||
// 新增 .. 目录项
|
||||
++new_dir_dentries;
|
||||
new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
|
||||
new_dir_dentries->DIR_FileSize = 0;
|
||||
new_dir_dentries->DIR_Name[0] = '.';
|
||||
new_dir_dentries->DIR_Name[1] = '.';
|
||||
for (int i = 2; i < 11; ++i)
|
||||
new_dir_dentries->DIR_Name[i] = 0x20;
|
||||
new_dir_dentries->DIR_FstClusHI = (unsigned short)(parent_inode_info->first_clus >> 16) & 0x0fff;
|
||||
new_dir_dentries->DIR_FstClusLO = (unsigned short)(parent_inode_info->first_clus) & 0xffff;
|
||||
|
||||
// 写入磁盘
|
||||
|
||||
uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus;
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
}
|
||||
// ===== 初始化inode ====
|
||||
|
||||
struct vfs_index_node_t *finode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
|
||||
memset(finode, 0, sizeof(struct vfs_index_node_t));
|
||||
finode->attribute = VFS_ATTR_DIR;
|
||||
finode->blocks = fsbi->sec_per_clus;
|
||||
finode->file_ops = &fat32_file_ops;
|
||||
finode->file_size = fsbi->bytes_per_clus;
|
||||
finode->inode_ops = &fat32_inode_ops;
|
||||
finode->sb = parent_inode->sb;
|
||||
finode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0);
|
||||
memset(finode->private_inode_info, 0, sizeof(fat32_inode_info_t));
|
||||
fat32_inode_info_t *p = (fat32_inode_info_t *)finode->private_inode_info;
|
||||
p->first_clus = new_dir_clus;
|
||||
p->dEntry_location_clus = tmp_parent_dentry_clus;
|
||||
p->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
|
||||
// todo: 填写fat32_inode_info的信息
|
||||
|
||||
// 初始化dentry信息
|
||||
list_init(&dEntry->child_node_list);
|
||||
list_init(&dEntry->subdirs_list);
|
||||
dEntry->dir_ops = &fat32_dEntry_ops;
|
||||
dEntry->dir_inode = finode;
|
||||
|
||||
// 注意:parent字段需要在调用函数的地方进行设置
|
||||
// 注意:需要将当前dentry加入父目录的subdirs_list
|
||||
|
||||
// 释放在find empty dentry中动态申请的缓冲区
|
||||
kfree((void *)tmp_dentry_clus_buf_addr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// todo: rmdir
|
||||
|
@ -489,7 +489,13 @@ uint64_t sys_chdir(struct pt_regs *regs)
|
||||
return -EFAULT;
|
||||
|
||||
// 计算输入的路径长度
|
||||
int dest_path_len = strnlen_user(dest_path, PAGE_4K_SIZE);
|
||||
int dest_path_len;
|
||||
if (regs->cs & USER_CS)
|
||||
{
|
||||
dest_path_len = strnlen_user(dest_path, PAGE_4K_SIZE);
|
||||
}
|
||||
else
|
||||
dest_path_len = strnlen(dest_path, PAGE_4K_SIZE);
|
||||
|
||||
// 长度小于等于0
|
||||
if (dest_path_len <= 0)
|
||||
@ -504,9 +510,13 @@ uint64_t sys_chdir(struct pt_regs *regs)
|
||||
return -ENOMEM;
|
||||
|
||||
memset(path, 0, dest_path_len + 1);
|
||||
|
||||
if (regs->cs & USER_CS)
|
||||
{
|
||||
// 将字符串从用户空间拷贝进来, +1是为了拷贝结尾的\0
|
||||
strncpy_from_user(path, dest_path, dest_path_len + 1);
|
||||
}
|
||||
else
|
||||
strncpy(path, dest_path, dest_path_len + 1);
|
||||
|
||||
struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
|
||||
|
||||
@ -605,7 +615,7 @@ uint64_t sys_wait4(struct pt_regs *regs)
|
||||
uint64_t pid = regs->r8;
|
||||
int *status = (int *)regs->r9;
|
||||
int options = regs->r10;
|
||||
void *rusage = (void*)regs->r11;
|
||||
void *rusage = (void *)regs->r11;
|
||||
|
||||
struct process_control_block *proc = NULL;
|
||||
struct process_control_block *child_proc = NULL;
|
||||
@ -651,7 +661,7 @@ uint64_t sys_wait4(struct pt_regs *regs)
|
||||
* @param exit_code 退出返回码
|
||||
* @return uint64_t
|
||||
*/
|
||||
uint64_t sys_exit(struct pt_regs * regs)
|
||||
uint64_t sys_exit(struct pt_regs *regs)
|
||||
{
|
||||
return process_do_exit(regs->r8);
|
||||
}
|
||||
@ -689,5 +699,6 @@ system_call_t system_call_table[MAX_SYSTEM_CALL_NUM] =
|
||||
[14] = sys_execve,
|
||||
[15] = sys_wait4,
|
||||
[16] = sys_exit,
|
||||
[17 ... 254] = system_call_not_exists,
|
||||
[17] = sys_mkdir,
|
||||
[18 ... 254] = system_call_not_exists,
|
||||
[255] = sys_ahci_end_req};
|
||||
|
@ -74,6 +74,15 @@ uint64_t sys_brk(struct pt_regs *regs);
|
||||
*/
|
||||
uint64_t sys_sbrk(struct pt_regs *regs);
|
||||
|
||||
/**
|
||||
* @brief 创建文件夹
|
||||
* 在VFS.c中实现
|
||||
* @param path(r8) 路径
|
||||
* @param mode(r9) 模式
|
||||
* @return uint64_t
|
||||
*/
|
||||
uint64_t sys_mkdir(struct pt_regs * regs);
|
||||
|
||||
ul sys_ahci_end_req(struct pt_regs *regs);
|
||||
|
||||
// 系统调用的内核入口程序
|
||||
|
@ -27,5 +27,6 @@
|
||||
#define SYS_EXECVE 14 // 执行新的应用程序
|
||||
#define SYS_WAIT4 15 // 等待进程退出
|
||||
#define SYS_EXIT 16 // 进程退出
|
||||
#define SYS_MKDIR 17 // 创建文件夹
|
||||
|
||||
#define SYS_AHCI_END_REQ 255 // AHCI DMA请求结束end_request的系统调用
|
@ -5,7 +5,7 @@
|
||||
#include <libKeyboard/keyboard.h>
|
||||
#include <libc/string.h>
|
||||
#include <libc/stddef.h>
|
||||
|
||||
#include <libc/sys/stat.h>
|
||||
#include "cmd.h"
|
||||
|
||||
#define pause_cpu() asm volatile("pause\n\t");
|
||||
@ -73,7 +73,9 @@ int main()
|
||||
int kb_fd = open(kb_file_path, 0);
|
||||
// printf("keyboard fd = %d\n", kb_fd);
|
||||
print_ascii_logo();
|
||||
|
||||
printf("before mkdir\n");
|
||||
mkdir("/aaac", 0);
|
||||
printf("after mkdir\n");
|
||||
main_loop(kb_fd);
|
||||
while (1)
|
||||
;
|
||||
|
@ -1,8 +1,11 @@
|
||||
|
||||
all: wait.o
|
||||
all: wait.o stat.o
|
||||
|
||||
CFLAGS += -I .
|
||||
|
||||
|
||||
wait.o: wait.c
|
||||
gcc $(CFLAGS) -c wait.c -o wait.o
|
||||
|
||||
stat.o: stat.c
|
||||
gcc $(CFLAGS) -c stat.c -o stat.o
|
7
user/libs/libc/sys/stat.c
Normal file
7
user/libs/libc/sys/stat.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include "stat.h"
|
||||
#include<libsystem/syscall.h>
|
||||
|
||||
int mkdir(const char *path, mode_t mode)
|
||||
{
|
||||
return syscall_invoke(SYS_MKDIR, (uint64_t)path, (uint64_t)mode, 0,0,0,0,0,0);
|
||||
}
|
4
user/libs/libc/sys/stat.h
Normal file
4
user/libs/libc/sys/stat.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <libc/sys/types.h>
|
||||
|
||||
int mkdir(const char *path, mode_t mode);
|
@ -21,6 +21,7 @@
|
||||
#define SYS_EXECVE 14 // 执行新的应用程序
|
||||
#define SYS_WAIT4 15 // 等待进程退出
|
||||
#define SYS_EXIT 16 // 进程退出
|
||||
#define SYS_MKDIR 17 // 创建文件夹
|
||||
|
||||
/**
|
||||
* @brief 用户态系统调用函数
|
||||
|
Reference in New Issue
Block a user