diff --git a/.vscode/settings.json b/.vscode/settings.json index f1e84dfb..ac1b53cc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -101,7 +101,8 @@ "stdio.h": "c", "wait_queue.h": "c", "stddef.h": "c", - "spinlock.h": "c" + "spinlock.h": "c", + "stat.h": "c" }, "C_Cpp.errorSquiggles": "Enabled", "esbonio.sphinx.confDir": "" diff --git a/docs/kernel/filesystem/vfs/overview.md b/docs/kernel/filesystem/vfs/overview.md index 4bb8c7b3..ffedc338 100644 --- a/docs/kernel/filesystem/vfs/overview.md +++ b/docs/kernel/filesystem/vfs/overview.md @@ -2,33 +2,35 @@ ## 简介 -​ DragonOS的虚拟文件系统是内核中的一层适配器,为用户程序(或者是系统程序)提供了通用的文件系统接口。同时对内核中的不同文件系统提供了统一的抽象。各种具体的文件系统可以挂载到VFS的框架之中。 +  DragonOS的虚拟文件系统是内核中的一层适配器,为用户程序(或者是系统程序)提供了通用的文件系统接口。同时对内核中的不同文件系统提供了统一的抽象。各种具体的文件系统可以挂载到VFS的框架之中。 -​ 与VFS相关的系统调用有open(), read(), write(), create()等。 +  与VFS相关的系统调用有open(), read(), write(), create()等。 ### dentry对象 -​ dentry的全称为directory entry,是VFS中对于目录项的一种抽象数据结构。当读取具体文件系统时,将会由创建dentry对象。dentry对象中包含了指向inode的指针。 +  dentry的全称为directory entry,是VFS中对于目录项的一种抽象数据结构。当读取具体文件系统时,将会由创建dentry对象。dentry对象中包含了指向inode的指针。 -​ dentry对象为真实文件系统上的目录结构建立了缓存,一旦内存中存在对应路径的dentry对象,我们就能直接获取其中的信息,而不需要进行费时的磁盘操作。请注意,dentry只是为提高文件系统性能而创建的一个缓存,它并不会被写入到磁盘之中。 +  dentry对象为真实文件系统上的目录结构建立了缓存,一旦内存中存在对应路径的dentry对象,我们就能直接获取其中的信息,而不需要进行费时的磁盘操作。请注意,dentry只是为提高文件系统性能而创建的一个缓存,它并不会被写入到磁盘之中。 ### inode对象 -​ inode的全称叫做index node,即索引节点。一般来说,每个dentry都应当包含指向其inode的阵阵。inode是VFS提供的对文件对象的抽象。inode中的信息是从具体文件系统中读取而来,也可以被刷回具体的文件系统之中。并且,一个inode也可以被多个dentry所引用。 +  inode的全称叫做index node,即索引节点。一般来说,每个dentry都应当包含指向其inode的指针。inode是VFS提供的对文件对象的抽象。inode中的信息是从具体文件系统中读取而来,也可以被刷回具体的文件系统之中。并且,一个inode也可以被多个dentry所引用。 -​ 要查找某个路径下的inode,我们需要调用父目录的inode的lookup()方法。请注意,该方法与具体文件系统有关,需要在具体文件系统之中实现。 +  要查找某个路径下的inode,我们需要调用父目录的inode的lookup()方法。请注意,该方法与具体文件系统有关,需要在具体文件系统之中实现。 ### 文件描述符对象 -​ 当一个进程试图通过VFS打开某个文件时,我们需要为这个进程创建文件描述符对象。每个文件对象都会绑定文件的dentry和文件操作方法结构体,还有文件对象的私有信息。 +  当一个进程试图通过VFS打开某个文件时,我们需要为这个进程创建文件描述符对象。每个文件对象都会绑定文件的dentry和文件操作方法结构体,还有文件对象的私有信息。 -​ 文件描述符对象中还包含了诸如权限控制、当前访问位置信息等内容,以便VFS对文件进行操作。 +  文件描述符对象中还包含了诸如权限控制、当前访问位置信息等内容,以便VFS对文件进行操作。 -​ 我们对文件进行操作都会使用到文件描述符,具体来说,就是要调用文件描述符之中的file_ops所包含的各种方法。 +  我们对文件进行操作都会使用到文件描述符,具体来说,就是要调用文件描述符之中的file_ops所包含的各种方法。 + +--- ## 注册文件系统到VFS -​ 如果需要注册或取消注册某个具体文件系统到VFS之中,则需要以下两个接口: +  如果需要注册或取消注册某个具体文件系统到VFS之中,则需要以下两个接口: ```c #include @@ -37,13 +39,13 @@ uint64_t vfs_register_filesystem(struct vfs_filesystem_type_t *fs); uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs); ``` -​ 这里需要通过`struct vfs_filesystem_type_t`来描述具体的文件系统。 +  这里需要通过`struct vfs_filesystem_type_t`来描述具体的文件系统。 ### struct vfs_filesystem_type_t -​ 这个数据结构描述了具体文件系统的一些信息。当我们挂载具体文件系统的时候,将会调用它的read_superblock方法,以确定要被挂载的文件系统的具体信息。 +  这个数据结构描述了具体文件系统的一些信息。当我们挂载具体文件系统的时候,将会调用它的read_superblock方法,以确定要被挂载的文件系统的具体信息。 -​ 该数据结构的定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: +  该数据结构的定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: ```c struct vfs_filesystem_type_t @@ -58,29 +60,31 @@ struct vfs_filesystem_type_t **name** -​ 文件系统名称字符串 +  文件系统名称字符串 **fs_flags** -​ 文件系统的一些标志位。目前,DragonOS尚未实现相关功能。 +  文件系统的一些标志位。目前,DragonOS尚未实现相关功能。 **read_superblock** -​ 当新的文件系统实例将要被挂载时,将会调用此方法,以读取具体的实例的信息。 +  当新的文件系统实例将要被挂载时,将会调用此方法,以读取具体的实例的信息。 **next** -​ 指向链表中下一个`struct vfs_filesystem_type_t`的指针。 +  指向链表中下一个`struct vfs_filesystem_type_t`的指针。 + +--- ## 超级块(superblock)对象 -    一个超级块对象代表了一个被挂载到VFS中的具体文件系统。 +  一个超级块对象代表了一个被挂载到VFS中的具体文件系统。 ### struct vfs_superblock_t -    该数据结构为超级块结构体。 +  该数据结构为超级块结构体。 -    该数据结构定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: +  该数据结构定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: ```c struct vfs_superblock_t @@ -93,21 +97,21 @@ struct vfs_superblock_t **root** -    该具体文件系统的根目录的dentry +  该具体文件系统的根目录的dentry **sb_ops** -    该超级块对象的操作方法。 +  该超级块对象的操作方法。 **private_sb_info** -    超级块的私有信息。包含了具体文件系统的私有的、全局性的信息。 +  超级块的私有信息。包含了具体文件系统的私有的、全局性的信息。 ### struct vfs_super_block_operations_t -    该数据结构为超级块的操作接口。 +  该数据结构为超级块的操作接口。VFS通过这些接口来操作具体的文件系统的超级块。 -    该数据结构定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: +  该数据结构定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: ```c struct vfs_super_block_operations_t @@ -120,4 +124,124 @@ struct vfs_super_block_operations_t **write_superblock** -     \ No newline at end of file +  将superblock中的信息写入磁盘 + +**put_superblock** + +  释放超级块 + +**write_inode** + +  将inode的信息写入磁盘 + +--- + +## 索引结点(inode)对象 + +  每个inode对象代表了具体的文件系统之中的一个对象(目录项)。 + +### struct vfs_index_node_t + +  该数据结构为inode对象的数据结构,与文件系统中的具体的文件结点对象具有一对一映射的关系。 + +  该数据结构定义在`kernel/filesystem/VFS/VFS.h`中,结构如下: + +```c +struct vfs_index_node_t +{ + uint64_t file_size; // 文件大小 + uint64_t blocks; // 占用的扇区数 + uint64_t attribute; + + struct vfs_superblock_t *sb; + struct vfs_file_operations_t *file_ops; + struct vfs_inode_operations_t *inode_ops; + + void *private_inode_info; +}; +``` + +**file_size** + +  文件的大小。若为文件夹,则该值为文件夹内所有文件的大小总和(估计值)。 + +**blocks** + +  文件占用的磁盘块数(扇区数) + +**attribute** + +  inode的属性。可选值如下: + +> - VFS_ATTR_FILE +> +> - VFS_ATTR_DIR +> +> - VFS_ATTR_DEVICE + +**sb** + +  指向文件系统超级块的指针 + +**file_ops** + +  当前文件的操作接口 + +**inode_ops** + +  当前inode的操作接口 + +**private_inode_info** + +  与具体文件系统相关的inode信息。该部分由具体文件系统实现,包含该inode在具体文件系统之中的特定格式信息。 + +### struct vfs_inode_operations_t + +  该接口为inode的操作方法接口,由具体文件系统实现。并与具体文件系统之中的inode相互绑定。 + +  该接口定义于`kernel/filesystem/VFS/VFS.h`中,结构如下: + +```c +struct vfs_inode_operations_t +{ + long (*create)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode); + struct vfs_dir_entry_t *(*lookup)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry); + 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); + long (*getAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr); + long (*setAttr)(struct vfs_dir_entry_t *dEntry, uint64_t *attr); +}; +``` + +**create** + +  在父节点下,创建一个新的inode,并绑定到dest_dEntry上。 + +  该函数的应当被`sys_open()`系统调用在使用了`O_CREAT`选项打开文件时调用,从而创建一个新的文件。请注意,传递给create()函数的`dest_dEntry`参数不应包含一个inode,也就是说,inode对象应当被具体文件系统所创建。 + +**lookup** + +  当VFS需要在父目录中查找一个inode的时候,将会调用lookup方法。被查找的目录项的名称将会通过dest_dEntry传给lookup方法。 + +  若lookup方法找到对应的目录项,将填充完善dest_dEntry对象。否则,返回NULL。 + +**mkdir** + +  该函数被mkdir()系统调用所调用,用于在inode下创建子目录,并将子目录的inode绑定到dEntry对象之中。 + +**rmdir** + +  该函数被rmdir()系统调用所调用,用于删除给定inode下的子目录项。 + +**rename** + +  该函数被rename系统调用(尚未实现)所调用,用于将给定的目录项重命名。 + +**getAttr** + +  用来获取目录项的属性。 + +**setAttr** + +  用来设置目录项的属性 \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index 117d5cbf..653e2e6d 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -18,7 +18,7 @@ LD_LIST := head.o OBJ_LIST := head.o -kernel_subdirs := common driver process debug +kernel_subdirs := common driver process debug filesystem @@ -78,14 +78,6 @@ cpu.o: common/cpu.c softirq.o: exception/softirq.c gcc $(CFLAGS) -c exception/softirq.c -o exception/softirq.o -fat32.o: filesystem/fat32/fat32.c - gcc $(CFLAGS) -c filesystem/fat32/fat32.c -o filesystem/fat32/fat32.o - -MBR.o: filesystem/MBR.c - gcc $(CFLAGS) -c filesystem/MBR.c -o filesystem/MBR.o - -VFS.o: filesystem/VFS/VFS.c - gcc $(CFLAGS) -c filesystem/VFS/VFS.c -o filesystem/VFS/VFS.o # IPI的代码 ifeq ($(ARCH), __x86_64__) @@ -164,7 +156,7 @@ all: kernel echo "Done." -kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.o sched.o syscall.o multiboot2.o cpu.o acpi.o ps2_keyboard.o ps2_mouse.o ata.o pci.o ahci.o smp.o apu_boot.o rtc.o HPET.o softirq.o timer.o fat32.o MBR.o VFS.o $(OBJ_LIST) +kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.o sched.o syscall.o multiboot2.o cpu.o acpi.o ps2_keyboard.o ps2_mouse.o ata.o pci.o ahci.o smp.o apu_boot.o rtc.o HPET.o softirq.o timer.o $(OBJ_LIST) @list='$(kernel_subdirs)'; for subdir in $$list; do \ echo "make all in $$subdir";\ diff --git a/kernel/filesystem/Makefile b/kernel/filesystem/Makefile new file mode 100644 index 00000000..c3287936 --- /dev/null +++ b/kernel/filesystem/Makefile @@ -0,0 +1,20 @@ + +CFLAGS += -I . + +all: fat32.o MBR.o VFS.o fat_ent.o + + +fat32.o: fat32/fat32.c + gcc $(CFLAGS) -c fat32/fat32.c -o fat32/fat32.o + +MBR.o: MBR.c + gcc $(CFLAGS) -c MBR.c -o MBR.o + +VFS.o: VFS/VFS.c + gcc $(CFLAGS) -c VFS/VFS.c -o VFS/VFS.o + +fat_ent.o: fat32/fat_ent.c + gcc $(CFLAGS) -c fat32/fat_ent.c -o fat32/fat_ent.o + +clean: + echo "Done." \ No newline at end of file diff --git a/kernel/filesystem/VFS/VFS.c b/kernel/filesystem/VFS/VFS.c index b1cd0c7f..db6bee88 100644 --- a/kernel/filesystem/VFS/VFS.c +++ b/kernel/filesystem/VFS/VFS.c @@ -90,7 +90,7 @@ struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags) return parent; struct vfs_dir_entry_t *dentry; - + // kdebug("path before walk:%s", path); while (true) { // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中 @@ -98,7 +98,6 @@ struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags) 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)); // 为目录项的名称分配内存 @@ -106,14 +105,15 @@ struct vfs_dir_entry_t *vfs_path_walk(const char *path, uint64_t flags) // 貌似这里不需要memset,因为空间会被覆盖 // memset(dentry->name, 0, tmp_path_len+1); - memcpy(dentry->name, (void*)tmp_path, tmp_path_len); + memcpy(dentry->name, (void *)tmp_path, tmp_path_len); dentry->name[tmp_path_len] = '\0'; + // kdebug("tmp_path_len=%d, dentry->name= %s", tmp_path_len, dentry->name); dentry->name_length = tmp_path_len; if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL) { // 搜索失败 - kerror("cannot find the file/dir : %s", dentry->name); + // kerror("cannot find the file/dir : %s", dentry->name); kfree(dentry->name); kfree(dentry); return NULL; @@ -179,7 +179,7 @@ int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned ch uint64_t sys_mkdir(struct pt_regs *regs) { const char *path = (const char *)regs->r8; - kdebug("path = %s", path); + // kdebug("path = %s", path); mode_t mode = (mode_t)regs->r9; uint32_t pathlen; if (regs->cs & USER_CS) @@ -215,11 +215,13 @@ uint64_t sys_mkdir(struct pt_regs *regs) else strncpy(buf, path, last_slash); buf[last_slash + 1] = '\0'; + // kdebug("to walk: %s", buf); // 查找父目录 struct vfs_dir_entry_t *parent_dir = vfs_path_walk(buf, 0); if (parent_dir == NULL) { + kwarn("parent dir is NULL."); kfree(buf); return -ENOENT; } @@ -249,12 +251,12 @@ uint64_t sys_mkdir(struct pt_regs *regs) } ++subdir_dentry->name_length; - kdebug("last_slash=%d", last_slash); - kdebug("name=%s", path + last_slash + 1); + // 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); + // 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); list_add(&parent_dir->subdirs_list, &subdir_dentry->child_node_list); - kdebug("retval = %d", retval); + // kdebug("retval = %d", retval); return 0; } \ No newline at end of file diff --git a/kernel/filesystem/VFS/VFS.h b/kernel/filesystem/VFS/VFS.h index a0746705..be43a0d4 100644 --- a/kernel/filesystem/VFS/VFS.h +++ b/kernel/filesystem/VFS/VFS.h @@ -12,6 +12,7 @@ #pragma once #include +#include struct vfs_superblock_t *vfs_root_sb = NULL; @@ -107,7 +108,13 @@ 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 父目录的inode结构体 + * @param dest_dEntry 新文件的dentry + * @param mode 创建模式 + */ + long (*create)(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode); /** * @brief 在文件系统中查找指定的目录项 * @param parent_inode 父目录项(在这个目录下查找) diff --git a/kernel/filesystem/fat32/fat32.c b/kernel/filesystem/fat32/fat32.c index fe2ac27c..cbfbe0fb 100644 --- a/kernel/filesystem/fat32/fat32.c +++ b/kernel/filesystem/fat32/fat32.c @@ -6,6 +6,7 @@ #include #include #include +#include "fat_ent.h" struct vfs_super_block_operations_t fat32_sb_ops; struct vfs_dir_entry_operations_t fat32_dEntry_ops; @@ -54,58 +55,6 @@ static uint8_t fat32_ChkSum(uint8_t *name) } return chksum; } -/** - * @brief 读取指定簇的FAT表项 - * - * @param fsbi fat32超级块私有信息结构体 - * @param cluster 指定簇 - * @return uint32_t 下一个簇的簇号 - */ -uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster) -{ - // 计算每个扇区内含有的FAT表项数 - // FAT每项4bytes - uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂 - - uint32_t buf[256]; - memset(buf, 0, fsbi->bytes_per_sec); - - // 读取一个sector的数据, - 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); - - // 返回下一个fat表项的值(也就是下一个cluster) - return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff; -} - -/** - * @brief 写入指定簇的FAT表项 - * - * @param fsbi fat32超级块私有信息结构体 - * @param cluster 指定簇 - * @param value 要写入该fat表项的值 - * @return uint32_t errcode - */ -uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value) -{ - // 计算每个扇区内含有的FAT表项数 - // FAT每项4bytes - uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂 - 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); - - 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); - 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); - kfree(buf); - return 0; -} /** * @brief 在父目录中寻找指定的目录项 @@ -190,6 +139,8 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru if (js >= dest_dentry->name_length) // 找到需要的目录项,返回 { + // kdebug("found target long name."); + goto find_lookup_success; } @@ -200,6 +151,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("no long name, comparing short name"); // kdebug("value = %#02x", tmp_dEntry->DIR_Name[x]); switch (tmp_dEntry->DIR_Name[x]) { @@ -265,11 +217,16 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru break; default: - ++js; + // ++js; + goto continue_cmp_fail; break; } } - + if (js > dest_dentry->name_length) + { + kdebug("js > namelen"); + goto continue_cmp_fail; + } // 若短目录项为文件,则匹配扩展名 if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY)) { @@ -320,6 +277,11 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru } } } + if (js > dest_dentry->name_length) + { + kdebug("js > namelen"); + goto continue_cmp_fail; + } goto find_lookup_success; continue_cmp_fail:; } @@ -665,38 +627,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos return retval; } -/** - * @brief 在磁盘中寻找一个空闲的簇 - * - * @param fsbi fat32超级块信息结构体 - * @return uint64_t 空闲簇号(找不到则返回0) - */ -uint64_t fat32_find_available_cluster(fat32_sb_info_t *fsbi) -{ - uint64_t sec_per_fat = fsbi->sec_per_FAT; - // 申请1扇区的缓冲区 - uint32_t *buf = (uint32_t *)kmalloc(fsbi->bytes_per_sec, 0); - int ent_per_sec = (fsbi->bytes_per_sec >> 2); - for (int i = 0; i < sec_per_fat; ++i) - { - memset(buf, 0, fsbi->bytes_per_sec); - - ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num); - // 依次检查簇是否空闲 - for (int j = 0; j < ent_per_sec; ++j) - { - // 找到空闲簇 - if ((buf[j] & 0x0fffffff) == 0) - { - kfree(buf); - return i * ent_per_sec + j; - } - } - } - kfree(buf); - return 0; -} /** * @brief 向fat32文件系统写入数据 @@ -712,11 +643,9 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info); // First cluster num of the file - uint64_t cluster = finode->first_clus; + uint32_t cluster = finode->first_clus; int64_t flags = 0; - // kdebug("fsbi->bytes_per_clus=%d fsbi->sec_per_clus=%d finode->first_clus=%d *position=%d", fsbi->bytes_per_clus, fsbi->sec_per_clus, finode->first_clus, *position); - // kdebug("buf=%s", buf); // clus offset in file uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus; // bytes offset in clus @@ -724,9 +653,9 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po if (!cluster) // 起始簇号为0,说明是空文件 { - // 找一个可用的簇 - cluster = fat32_find_available_cluster(fsbi); - flags = 1; + // 分配空闲簇 + if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &cluster, 1) != 0) + return -ENOSPC; } else { @@ -739,14 +668,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po if (!cluster) return -ENOSPC; - if (flags) // 空文件 - { - // kdebug("empty file"); - finode->first_clus = cluster; - // 写入目录项 - file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode); - fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8); // 写入fat表项 - } + int64_t bytes_remain = count; @@ -809,15 +731,13 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po break; if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇 { - next_clus = fat32_find_available_cluster(fsbi); - if (!next_clus) // 没有空闲簇 + if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &next_clus, 1) != 0) { + // 没有空闲簇 kfree(tmp_buffer); return -ENOSPC; } - // 将簇加入到文件末尾 - fat32_write_FAT_entry(fsbi, cluster, next_clus); - fat32_write_FAT_entry(fsbi, next_clus, 0x0ffffff8); + cluster = next_clus; // 切换当前簇 flags = 1; // 标记当前簇是新分配的簇 } @@ -829,7 +749,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po { file_ptr->dEntry->dir_inode->file_size = *position; file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode); - kdebug("new file size=%ld", *position); + // kdebug("new file size=%ld", *position); } kfree(tmp_buffer); @@ -896,120 +816,48 @@ struct vfs_file_operations_t fat32_file_ops = .readdir = fat32_readdir, }; -// todo: create -long fat32_create(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个≤这个指针的空目录项) + * @brief 创建新的文件 + * @param parent_inode 父目录的inode结构体 + * @param dest_dEntry 新文件的dentry + * @param mode 创建模式 */ -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) +long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode) { - 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 + // 父目录项的inode的私有信息 struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info; - // ======== todo:检验名称的合法性 - // ====== 找一块连续的区域放置新的目录项 ===== + int64_t retval = 0; + + // ======== 检验名称的合法性 + retval = fat32_check_name_available(dest_dEntry->name, dest_dEntry->name_length, 0); + + if (retval != 0) + return retval; + + if (dest_dEntry->dir_inode != NULL) + return -EEXIST; + + struct vfs_index_node_t *inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0); + memset((void *)inode, 0, sizeof(struct vfs_index_node_t)); + dest_dEntry->dir_inode = inode; + dest_dEntry->dir_ops = &fat32_dEntry_ops; + + struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)kmalloc(sizeof(struct fat32_inode_info_t), 0); + memset((void *)finode, 0, sizeof(struct fat32_inode_info_t)); + inode->attribute = VFS_ATTR_FILE; + inode->file_ops = &fat32_file_ops; + inode->file_size = 0; + inode->sb = parent_inode->sb; + inode->inode_ops = &fat32_inode_ops; + inode->private_inode_info = (void *)finode; + inode->blocks = fsbi->sec_per_clus; // 计算总共需要多少个目录项 - uint32_t cnt_longname = (dEntry->name_length + 25) / 26; + uint32_t cnt_longname = (dest_dEntry->name_length + 25) / 26; + // 默认都是创建长目录项来存储 if (cnt_longname == 0) cnt_longname = 1; @@ -1020,82 +868,139 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_ 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); + // kdebug("found empty dentry, cnt_longname=%ld", cnt_longname); - // ====== 填写短目录项 - memset(empty_fat32_dentry, 0, sizeof(struct fat32_Directory_t)); + finode->first_clus = 0; + finode->dEntry_location_clus = tmp_parent_dentry_clus; + finode->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr; + + // ====== 为新的文件分配一个簇 ======= + uint32_t new_dir_clus; + if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0) { - 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; - } + retval = -ENOSPC; + goto fail; } - 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); + // kdebug("new dir clus=%ld", new_dir_clus); + // kdebug("dest_dEntry->name=%s",dest_dEntry->name); + // ====== 填写短目录项 + fat32_fill_shortname(dest_dEntry, empty_fat32_dentry, new_dir_clus); + // kdebug("dest_dEntry->name=%s",dest_dEntry->name); // 计算校验和 uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name); - // todo: 填写短目录项中的时间信息 + // kdebug("dest_dEntry->name=%s",dest_dEntry->name); // ======== 填写长目录项 - 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; + fat32_fill_longname(dest_dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname); // ====== 将目录项写回磁盘 + // kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector); + 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); + + // 注意:parent字段需要在调用函数的地方进行设置 + + // 释放在find empty dentry中动态申请的缓冲区 + kfree((void *)tmp_dentry_clus_buf_addr); + return 0; +fail:; + // 释放在find empty dentry中动态申请的缓冲区 + kfree((void *)tmp_dentry_clus_buf_addr); + dest_dEntry->dir_inode = NULL; + dest_dEntry->dir_ops = NULL; + kfree(finode); + kfree(inode); + return retval; +} + +/** + * @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) +{ + int64_t retval = 0; + + // 文件系统超级块信息 + 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; + // ======== 检验名称的合法性 + retval = fat32_check_name_available(dEntry->name, dEntry->name_length, 0); + if (retval != 0) + return retval; + // ====== 找一块连续的区域放置新的目录项 ===== + + // 计算总共需要多少个目录项 + 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); + + + // ====== 初始化inode ======= + struct vfs_index_node_t *inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0); + memset(inode, 0, sizeof(struct vfs_index_node_t)); + inode->attribute = VFS_ATTR_DIR; + inode->blocks = fsbi->sec_per_clus; + inode->file_ops = &fat32_file_ops; + inode->file_size = 0; + inode->inode_ops = &fat32_inode_ops; + inode->sb = parent_inode->sb; + + // ===== 初始化inode的文件系统私有信息 ==== + + inode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0); + memset(inode->private_inode_info, 0, sizeof(fat32_inode_info_t)); + fat32_inode_info_t *p = (fat32_inode_info_t *)inode->private_inode_info; + p->first_clus = 0; + 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; + // kdebug(" p->dEntry_location_clus_offset=%d", p->dEntry_location_clus_offset); + // 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 = inode; + + // ====== 为新的文件夹分配一个簇 ======= + uint32_t new_dir_clus; + if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0) + { + retval = -ENOSPC; + goto fail; + } + + // kdebug("new dir clus=%ld", new_dir_clus); + + // ====== 填写短目录项 + fat32_fill_shortname(dEntry, empty_fat32_dentry, new_dir_clus); + + // 计算校验和 + uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name); + + // ======== 填写长目录项 + fat32_fill_longname(dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname); + + // ====== 将目录项写回磁盘 + // kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector); 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); // ====== 初始化新的文件夹的目录项 ===== { + // kdebug("to create dot and dot dot."); 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); @@ -1106,7 +1011,7 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_ 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; @@ -1124,31 +1029,9 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_ // 写入磁盘 uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus; + // kdebug("add dot and dot dot: sector=%ld", sector); 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 @@ -1157,6 +1040,10 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_ kfree((void *)tmp_dentry_clus_buf_addr); return 0; +fail:; + // 释放在find empty dentry中动态申请的缓冲区 + kfree((void *)tmp_dentry_clus_buf_addr); + return retval; } // todo: rmdir diff --git a/kernel/filesystem/fat32/fat32.h b/kernel/filesystem/fat32/fat32.h index f73fffde..ec592d7c 100644 --- a/kernel/filesystem/fat32/fat32.h +++ b/kernel/filesystem/fat32/fat32.h @@ -155,7 +155,7 @@ typedef struct fat32_partition_info_t fat32_sb_info_t; struct fat32_inode_info_t { - uint64_t first_clus; // 文件的起始簇号 + uint32_t first_clus; // 文件的起始簇号 uint64_t dEntry_location_clus; // fat entry的起始簇号 dEntry struct in cluster (0 is root, 1 is invalid) uint64_t dEntry_location_clus_offset; // fat entry在起始簇中的偏移量(是第几个entry) dEntry struct offset in cluster @@ -176,7 +176,6 @@ typedef struct fat32_inode_info_t fat32_inode_info_t; */ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num); - /** * @brief 创建fat32文件系统的超级块 * @@ -187,7 +186,13 @@ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t */ struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num); -long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode); +/** + * @brief 创建新的文件 + * @param parent_inode 父目录的inode结构体 + * @param dest_dEntry 新文件的dentry + * @param mode 创建模式 + */ +long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode); void fat32_init(); diff --git a/kernel/filesystem/fat32/fat_ent.c b/kernel/filesystem/fat32/fat_ent.c new file mode 100644 index 00000000..c88ed8cd --- /dev/null +++ b/kernel/filesystem/fat32/fat_ent.c @@ -0,0 +1,414 @@ +#include "fat_ent.h" +#include +#include +#include + +static const char unavailable_character_in_short_name[] = {0x22, 0x2a, 0x2b, 0x2c, 0x2e, 0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x5b, 0x5c, 0x5d, 0x7c}; +/** + * @brief 请求分配指定数量的簇 + * + * @param inode 要分配簇的inode + * @param clusters 返回的被分配的簇的簇号结构体 + * @param num_clusters 要分配的簇的数量 + * @return int 错误码 + */ +int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int32_t num_clusters) +{ + int retval = 0; + + fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info; + struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)inode->private_inode_info; + + uint64_t sec_per_fat = fsbi->sec_per_FAT; + + // todo: 对alloc的过程加锁 + + // 申请1扇区的缓冲区 + uint32_t *buf = (uint32_t *)kmalloc(fsbi->bytes_per_sec, 0); + int ent_per_sec = (fsbi->bytes_per_sec >> 2); + int clus_idx = 0; + for (int i = 0; i < sec_per_fat; ++i) + { + if (clus_idx >= num_clusters) + goto done; + memset(buf, 0, fsbi->bytes_per_sec); + + ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num); + // 依次检查簇是否空闲 + for (int j = 0; j < ent_per_sec; ++j) + { + if (clus_idx >= num_clusters) + goto done; + // 找到空闲簇 + if ((buf[j] & 0x0fffffff) == 0) + { + // kdebug("clus[%d] = %d", clus_idx, i * ent_per_sec + j); + clusters[clus_idx] = i * ent_per_sec + j; + ++clus_idx; + } + } + } + // 空间不足 + retval = -ENOSPC; + +done:; + kfree(buf); + if (retval == 0) // 成功 + { + int cluster, idx; + if (finode->first_clus == 0) + { + // 空文件 + finode->first_clus = clusters[0]; + cluster = finode->first_clus; + // 写入inode到磁盘 + inode->sb->sb_ops->write_inode(inode); + idx = 1; + } + else + { + // todo: 跳转到文件当前的最后一个簇 + idx = 0; + int tmp_clus = finode->first_clus; + cluster = tmp_clus; + while (true) + { + tmp_clus = fat32_read_FAT_entry(fsbi, cluster); + if (tmp_clus <= 0x0ffffff7) + cluster = tmp_clus; + else + break; + } + } + + // 写入fat表 + for (int i = idx; i < num_clusters; ++i) + { + // kdebug("write cluster i=%d : cluster=%d, value= %d", i, cluster, clusters[i]); + fat32_write_FAT_entry(fsbi, cluster, clusters[i]); + cluster = clusters[i]; + } + fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8); + + return 0; + } + else // 出现错误 + { + kwarn("err in alloc clusters"); + if (clus_idx < num_clusters) + fat32_free_clusters(inode, clusters[0]); + return retval; + } + + return 0; +} + +/** + * @brief 释放从属于inode的,从cluster开始的所有簇 + * + * @param inode 指定的文件的inode + * @param cluster 指定簇 + * @return int 错误码 + */ +int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster) +{ + // todo: 释放簇 + return 0; +} + +/** + * @brief 读取指定簇的FAT表项 + * + * @param fsbi fat32超级块私有信息结构体 + * @param cluster 指定簇 + * @return uint32_t 下一个簇的簇号 + */ +uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster) +{ + // 计算每个扇区内含有的FAT表项数 + // FAT每项4bytes + uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂 + + uint32_t buf[256]; + memset(buf, 0, fsbi->bytes_per_sec); + + // 读取一个sector的数据, + 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); + + // 返回下一个fat表项的值(也就是下一个cluster) + return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff; +} + +/** + * @brief 写入指定簇的FAT表项 + * + * @param fsbi fat32超级块私有信息结构体 + * @param cluster 指定簇 + * @param value 要写入该fat表项的值 + * @return uint32_t errcode + */ +uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value) +{ + // 计算每个扇区内含有的FAT表项数 + // FAT每项4bytes + uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂 + 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); + + 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); + 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); + kfree(buf); + return 0; +} + +/** + * @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; + } + + // 当前簇没有发现符合条件的空闲目录项,寻找下一个簇 + uint64_t old_cluster = cluster; + cluster = fat32_read_FAT_entry(fsbi, cluster); + if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到符合要求的空目录项 + { + + // 新增一个簇 + + if (fat32_alloc_clusters(parent_inode, &cluster, 1) != 0) + { + kerror("Cannot allocate a new cluster!"); + while (1) + pause(); + } + + // 将这个新的簇清空 + 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 name 文件名 + * @param namelen 文件名长度 + * @param reserved 保留字段 + * @return int 合法:0, 其他:错误码 + */ +int fat32_check_name_available(const char *name, int namelen, int8_t reserved) +{ + if (namelen > 255 || namelen <= 0) + return -ENAMETOOLONG; + // 首个字符不能是空格或者'.' + if (name[0] == 0x20 || name[0] == '.') + return -EINVAL; + + return 0; +} + +/** + * @brief 检查字符在短目录项中是否合法 + * + * @param c 给定字符 + * @param index 字符在文件名中处于第几位 + * @return true 合法 + * @return false 不合法 + */ +bool fat32_check_char_available_in_short_name(const char c, int index) +{ + // todo: 严格按照fat规范完善合法性检查功能 + if (index == 0) + { + if (c < 0x20) + { + if (c != 0x05) + return false; + return true; + } + } + + for (int i = 0; i < sizeof(unavailable_character_in_short_name) / sizeof(char); ++i) + { + if (c == unavailable_character_in_short_name[i]) + return false; + } + return true; +} + +/** + * @brief 填充短目录项的函数 + * + * @param dEntry 目标dentry + * @param target 目标dentry对应的短目录项 + * @param cluster 短目录项对应的文件/文件夹起始簇 + */ +void fat32_fill_shortname(struct vfs_dir_entry_t *dEntry, struct fat32_Directory_t *target, uint32_t cluster) +{ + memset(target, 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; + if (fat32_check_char_available_in_short_name(dEntry->name[tmp_index], tmp_index)) + target->DIR_Name[tmp_index] = dEntry->name[tmp_index]; + else + target->DIR_Name[tmp_index] = 0x20; + } + + // 不满的部分使用0x20填充 + while (tmp_index < 8) + { + // kdebug("tmp index = %d", tmp_index); + target->DIR_Name[tmp_index] = 0x20; + ++tmp_index; + } + if (dEntry->dir_inode->attribute & VFS_ATTR_DIR) + { + while (tmp_index < 11) + { + // kdebug("tmp index = %d", tmp_index); + target->DIR_Name[tmp_index] = 0x20; + ++tmp_index; + } + } + else + { + for(int j = 8;j<11;++j) + { + target->DIR_Name[j] = 'a'; + } + } + } + + struct vfs_index_node_t *inode = dEntry->dir_inode; + target->DIR_Attr = 0; + if (inode->attribute & VFS_ATTR_DIR) + target->DIR_Attr |= ATTR_DIRECTORY; + + target->DIR_FileSize = dEntry->dir_inode->file_size; + target->DIR_FstClusHI = (uint16_t)((cluster >> 16) & 0x0fff); + target->DIR_FstClusLO = (uint16_t)(cluster & 0xffff); + + // todo: 填写短目录项中的时间信息 +} + +/** + * @brief 填充长目录项的函数 + * + * @param dEntry 目标dentry + * @param target 起始长目录项 + * @param checksum 短目录项的校验和 + * @param cnt_longname 总的长目录项的个数 + */ +void fat32_fill_longname(struct vfs_dir_entry_t *dEntry, struct fat32_LongDirectory_t *target, uint8_t checksum, uint32_t cnt_longname) +{ + uint32_t current_name_index = 0; + struct fat32_LongDirectory_t *Ldentry = (struct fat32_LongDirectory_t *)(target + 1); + // kdebug("filling long name, name=%s, namelen=%d", dEntry->name, dEntry->name_length); + 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 = checksum; + } + + // 最后一个长目录项的ord要|=0x40 + Ldentry->LDIR_Ord |= 0x40; +} \ No newline at end of file diff --git a/kernel/filesystem/fat32/fat_ent.h b/kernel/filesystem/fat32/fat_ent.h new file mode 100644 index 00000000..23fe5100 --- /dev/null +++ b/kernel/filesystem/fat32/fat_ent.h @@ -0,0 +1,95 @@ +#pragma once + +#include "fat32.h" +#include +#include + +/** + * @brief 请求分配指定数量的簇 + * + * @param inode 要分配簇的inode + * @param clusters 返回的被分配的簇的簇号结构体 + * @param num_clusters 要分配的簇的数量 + * @return int 错误码 + */ +int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int32_t num_clusters); + +/** + * @brief 释放从属于inode的,从cluster开始的所有簇 + * + * @param inode 指定的文件的inode + * @param cluster 指定簇 + * @return int 错误码 + */ +int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster); + +/** + * @brief 读取指定簇的FAT表项 + * + * @param fsbi fat32超级块私有信息结构体 + * @param cluster 指定簇 + * @return uint32_t 下一个簇的簇号 + */ +uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster); + +/** + * @brief 写入指定簇的FAT表项 + * + * @param fsbi fat32超级块私有信息结构体 + * @param cluster 指定簇 + * @param value 要写入该fat表项的值 + * @return uint32_t errcode + */ +uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value); + +/** + * @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); + +/** + * @brief 检查文件名是否合法 + * + * @param name 文件名 + * @param namelen 文件名长度 + * @param reserved 保留字段 + * @return int 合法:0, 其他:错误码 + */ +int fat32_check_name_available(const char *name, int namelen, int8_t reserved); + +/** + * @brief 检查字符在短目录项中是否合法 + * + * @param c 给定字符 + * @param index 字符在文件名中处于第几位 + * @return true 合法 + * @return false 不合法 + */ +bool fat32_check_char_available_in_short_name(const char c, int index); + +/** + * @brief 填充短目录项的函数 + * + * @param dEntry 目标dentry + * @param target 目标dentry对应的短目录项 + * @param cluster 短目录项对应的文件/文件夹起始簇 + */ +void fat32_fill_shortname(struct vfs_dir_entry_t *dEntry, struct fat32_Directory_t *target, uint32_t cluster); + +/** + * @brief 填充长目录项的函数 + * + * @param dEntry 目标dentry + * @param target 起始长目录项 + * @param checksum 短目录项的校验和 + * @param cnt_longname 总的长目录项的个数 + */ +void fat32_fill_longname(struct vfs_dir_entry_t *dEntry, struct fat32_LongDirectory_t *target, uint8_t checksum, uint32_t cnt_longname); \ No newline at end of file diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 588a362c..00ef1557 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -108,7 +108,6 @@ ul sys_put_string(struct pt_regs *regs) uint64_t sys_open(struct pt_regs *regs) { - char *filename = (char *)(regs->r8); int flags = (int)(regs->r9); // kdebug("filename=%s", filename); @@ -131,6 +130,12 @@ uint64_t sys_open(struct pt_regs *regs) memset(path, 0, path_len); strncpy_from_user(path, filename, path_len); + // 去除末尾的 '/' + if (path_len >= 2 && path[path_len - 2] == '/') + { + path[path_len - 2] = '\0'; + --path_len; + } // 寻找文件 struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0); @@ -139,7 +144,61 @@ uint64_t sys_open(struct pt_regs *regs) // printk_color(ORANGE, BLACK, "Found %s\nDIR_FstClus:%#018lx\tDIR_FileSize:%#018lx\n", path, ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus, dentry->dir_inode->file_size); // else // printk_color(ORANGE, BLACK, "Can`t find file\n"); + // kdebug("flags=%#018lx", flags); + if (dentry == NULL && flags & O_CREAT) + { + // 先找到倒数第二级目录 + int tmp_index = -1; + for (int i = path_len - 1; i >= 0; --i) + { + if (path[i] == '/') + { + tmp_index = i; + break; + } + } + struct vfs_dir_entry_t *parent_dentry = NULL; + // kdebug("tmp_index=%d", tmp_index); + if (tmp_index > 0) + { + + path[tmp_index] = '\0'; + dentry = vfs_path_walk(path, 0); + if (dentry == NULL) + { + kfree(path); + return -ENOENT; + } + parent_dentry = dentry; + } + else + parent_dentry = vfs_root_sb->root; + + // 创建新的文件 + 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_length = path_len - tmp_index - 1; + dentry->name = (char *)kmalloc(dentry->name_length, 0); + memset(dentry->name, 0, dentry->name_length); + strncpy(dentry->name, path + tmp_index + 1, dentry->name_length); + // kdebug("to create new file:%s namelen=%d", dentry->name, dentry->name_length) + dentry->parent = parent_dentry; + uint64_t retval = parent_dentry->dir_inode->inode_ops->create(parent_dentry->dir_inode, dentry, 0); + if (retval != 0) + { + kfree(dentry->name); + kfree(dentry); + kfree(path); + return retval; + } + + list_init(&dentry->child_node_list); + list_init(&dentry->subdirs_list); + list_add(&parent_dentry->subdirs_list, &dentry->child_node_list); + // kdebug("created."); + } kfree(path); if (dentry == NULL) return -ENOENT; @@ -148,18 +207,21 @@ uint64_t sys_open(struct pt_regs *regs) if ((flags & O_DIRECTORY) && (dentry->dir_inode->attribute != VFS_ATTR_DIR)) return -ENOTDIR; - // 要找的目标是文件夹 - if ((flags & O_DIRECTORY) && dentry->dir_inode->attribute == VFS_ATTR_DIR) - return -EISDIR; + // // 要找的目标是文件夹 + // if ((flags & O_DIRECTORY) && dentry->dir_inode->attribute == VFS_ATTR_DIR) + // return -EISDIR; // todo: 引入devfs后删除这段代码 // 暂时遇到设备文件的话,就将其first clus设置为特定值 if (path_len >= 5 && filename[0] == '/' && filename[1] == 'd' && filename[2] == 'e' && filename[3] == 'v' && filename[4] == '/') { - // 对于fat32文件系统上面的设备文件,设置其起始扇区 - ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus |= 0xf0000000; - dentry->dir_inode->sb->sb_ops->write_inode(dentry->dir_inode); - dentry->dir_inode->attribute |= VFS_ATTR_DEVICE; + if (dentry->dir_inode->attribute & VFS_ATTR_FILE) + { + // 对于fat32文件系统上面的设备文件,设置其起始扇区 + ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus |= 0xf0000000; + dentry->dir_inode->sb->sb_ops->write_inode(dentry->dir_inode); + dentry->dir_inode->attribute |= VFS_ATTR_DEVICE; + } } // 创建文件描述符 @@ -404,12 +466,7 @@ uint64_t sys_brk(struct pt_regs *regs) else offset = -(int64_t)(current_pcb->mm->brk_end - new_brk); - /* - if (offset < 0) - { - kdebug("decrease brk, offset = %#010lx", (uint64_t)(-offset)); - } - */ + new_brk = mm_do_brk(current_pcb->mm->brk_end, offset); // 扩展堆内存空间 @@ -517,14 +574,14 @@ uint64_t sys_chdir(struct pt_regs *regs) } else strncpy(path, dest_path, dest_path_len + 1); - + // kdebug("chdir: path = %s", path); struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0); kfree(path); if (dentry == NULL) return -ENOENT; - + // kdebug("dentry->name=%s, namelen=%d", dentry->name, dentry->name_length); // 目标不是目录 if (dentry->dir_inode->attribute != VFS_ATTR_DIR) return -ENOTDIR; diff --git a/run.sh b/run.sh index 07a95aa4..97121e36 100644 --- a/run.sh +++ b/run.sh @@ -91,6 +91,7 @@ fi # 拷贝应用程序到硬盘 cd tools bash m* +sudo mkdir -p ${root_folder}/bin/disk_mount sudo cp ${root_folder}/bin/user/shell.elf ${root_folder}/bin/disk_mount sudo cp ${root_folder}/bin/user/about.elf ${root_folder}/bin/disk_mount sudo mkdir ${root_folder}/bin/disk_mount/dev diff --git a/tools/create_hdd_image.sh b/tools/create_hdd_image.sh index ef450be0..79ed5116 100755 --- a/tools/create_hdd_image.sh +++ b/tools/create_hdd_image.sh @@ -5,6 +5,12 @@ qemu-img create -f raw disk.img 16M # 按顺序输入,并且,每次输入完成后要按下回车) fdisk disk.img -echo "Successfully created disk image, please make a FAT32 filesystem on it" -sudo mkdir -p ../bin -sudo cp ./disk.img ../bin/ +LOOP_DEVICE=$(sudo losetup -f --show -P disk.img) \ + || exit 1 +echo ${LOOP_DEVICE}p1 +sudo mkfs.vfat -F 32 ${LOOP_DEVICE}p1 +sudo losetup -d ${LOOP_DEVICE} + +echo "Successfully created disk image." +mkdir -p ../bin +mv ./disk.img ../bin/ diff --git a/tools/mount_virt_disk.sh b/tools/mount_virt_disk.sh index 8a7314ce..1eb06565 100755 --- a/tools/mount_virt_disk.sh +++ b/tools/mount_virt_disk.sh @@ -2,7 +2,7 @@ LOOP_DEVICE=$(sudo losetup -f --show -P ../bin/disk.img) \ || exit 1 echo ${LOOP_DEVICE}p1 -sudo mkfs.vfat -F 32 ${LOOP_DEVICE}p1 + mkdir -p ../bin/disk_mount/ sudo mount ${LOOP_DEVICE}p1 ../bin/disk_mount/ lsblk \ No newline at end of file diff --git a/user/apps/shell/cmd.c b/user/apps/shell/cmd.c index f37ffbe8..db72508b 100644 --- a/user/apps/shell/cmd.c +++ b/user/apps/shell/cmd.c @@ -10,7 +10,7 @@ #include #include #include - +#include #include "cmd_help.h" // 当前工作目录(在main_loop中初始化) @@ -181,7 +181,7 @@ int shell_cmd_cd(int argc, char **argv) goto fail; ; // 出错则直接忽略 } - else + else // ======进入相对路径===== { int dest_offset = 0; if (dest_len > 2) @@ -191,7 +191,7 @@ int shell_cmd_cd(int argc, char **argv) } int new_len = current_dir_len + dest_len - dest_offset; - // ======进入相对路径===== + if (new_len >= SHELL_CWD_MAX_SIZE - 1) { printf("ERROR: Path too long!\n"); @@ -210,7 +210,8 @@ int shell_cmd_cd(int argc, char **argv) if (chdir(new_path) == 0) // 成功切换目录 { free(shell_current_path); - new_path[new_len] = '\0'; + // printf("new_path=%s, newlen= %d\n", new_path, new_len); + new_path[new_len + 1] = '\0'; shell_current_path = new_path; goto done; } @@ -329,8 +330,30 @@ int shell_cmd_cat(int argc, char **argv) * @param argv * @return int */ -// todo: -int shell_cmd_touch(int argc, char **argv) {} +int shell_cmd_touch(int argc, char **argv) +{ + int path_len = 0; + char *file_path; + if (argv[1][0] == '/') + file_path = argv[1]; + else + file_path = get_target_filepath(argv[1], &path_len); + + // 打开文件 + int fd = open(file_path, O_CREAT); + switch (fd) + { + case -ENOENT: + put_string("Parent dir not exists.\n", COLOR_RED, COLOR_BLACK); + break; + + default: + break; + } + close(fd); + if (argv != NULL) + free(argv); +} /** * @brief 删除命令 @@ -349,8 +372,24 @@ int shell_cmd_rm(int argc, char **argv) {} * @param argv * @return int */ -// todo: -int shell_cmd_mkdir(int argc, char **argv) {} +int shell_cmd_mkdir(int argc, char **argv) +{ + int result_path_len = -1; + const char *full_path = NULL; + if (argv[1][0] == '/') + full_path = argv[1]; + else + { + full_path = get_target_filepath(argv[1], &result_path_len); + } + printf("mkdir: full_path = %s\n", full_path); + int retval = mkdir(full_path, 0); + + if (argv != NULL) + free(argv); + + return retval; +} /** * @brief 删除文件夹的命令 @@ -406,13 +445,12 @@ int shell_cmd_about(int argc, char **argv) char **aav; unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; - + strcpy(input_buffer, "exec /about.elf\0"); parse_command(input_buffer, &aac, &aav); shell_cmd_exec(aac, aav); - } /** diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index dfacafb0..4f6c99cf 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -73,9 +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"); + // printf("before mkdir\n"); + // mkdir("/aaac", 0); + // printf("after mkdir\n"); main_loop(kb_fd); while (1) ; diff --git a/user/libs/libc/fcntl.c b/user/libs/libc/fcntl.c index 1e7630e7..98554838 100644 --- a/user/libs/libc/fcntl.c +++ b/user/libs/libc/fcntl.c @@ -11,5 +11,5 @@ */ int open(const char *path, int options, ...) { - return syscall_invoke(SYS_OPEN, (uint64_t)path, 0, 0, 0, 0, 0, 0, 0); + return syscall_invoke(SYS_OPEN, (uint64_t)path, options, 0, 0, 0, 0, 0, 0); } \ No newline at end of file