From 86a5f25e07528390312d1b3ca47b3c8d04b05be1 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Tue, 6 Sep 2022 15:12:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAvfs=E6=8C=82=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F=E5=A2=9E=E5=8A=A0=E6=8C=82=E8=BD=BD?= =?UTF-8?q?=E7=82=B9=E8=B7=AF=E5=BE=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/filesystem/VFS/VFS.c | 14 ++++++++++---- kernel/filesystem/VFS/VFS.h | 9 +++++---- kernel/filesystem/fat32/fat32.c | 14 +++++++------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/kernel/filesystem/VFS/VFS.c b/kernel/filesystem/VFS/VFS.c index bd312631..27489a90 100644 --- a/kernel/filesystem/VFS/VFS.c +++ b/kernel/filesystem/VFS/VFS.c @@ -10,25 +10,31 @@ // 为filesystem_type_t结构体实例化一个链表头 static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0}; +struct vfs_superblock_t *vfs_root_sb = NULL; /** * @brief 挂载文件系统 * + * @param path 要挂载到的路径 * @param name 文件系统名 * @param blk 块设备结构体 - * @return struct vfs_superblock_t* + * @return struct vfs_superblock_t* 挂载后,文件系统的超级块 */ -struct vfs_superblock_t *vfs_mount_fs(char *name, struct block_device *blk) +struct vfs_superblock_t *vfs_mount_fs(const char *path, char *name, struct block_device *blk) { - + // todo: 选择挂载点 struct vfs_filesystem_type_t *p = NULL; for (p = &vfs_fs; p; p = p->next) { if (!strcmp(p->name, name)) // 存在符合的文件系统 { - return p->read_superblock(blk); + struct vfs_superblock_t *sb = p->read_superblock(blk); + if (strcmp(path, "/") == 0) // 如果挂载到的是'/'挂载点,则让其成为最顶层的文件系统 + vfs_root_sb = sb; + return sb; } } + kdebug("unsupported fs: %s", name); return NULL; } diff --git a/kernel/filesystem/VFS/VFS.h b/kernel/filesystem/VFS/VFS.h index 3007e216..0de27e06 100644 --- a/kernel/filesystem/VFS/VFS.h +++ b/kernel/filesystem/VFS/VFS.h @@ -15,7 +15,7 @@ #include #include -struct vfs_superblock_t *vfs_root_sb = NULL; +extern struct vfs_superblock_t *vfs_root_sb; #define VFS_DPT_MBR 0 // MBR分区表 #define VFS_DPT_GPT 1 // GPT分区表 @@ -54,7 +54,7 @@ struct vfs_superblock_t { struct vfs_dir_entry_t *root; struct vfs_super_block_operations_t *sb_ops; - struct block_device * blk_device; + struct block_device *blk_device; void *private_sb_info; }; @@ -175,11 +175,12 @@ uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs); /** * @brief 挂载文件系统 * + * @param path 要挂载到的路径 * @param name 文件系统名 * @param blk 块设备结构体 - * @return struct vfs_superblock_t* + * @return struct vfs_superblock_t* 挂载后,文件系统的超级块 */ -struct vfs_superblock_t *vfs_mount_fs(char *name, struct block_device *blk); +struct vfs_superblock_t *vfs_mount_fs(const char *path, char *name, struct block_device *blk); /** * @brief 按照路径查找文件 diff --git a/kernel/filesystem/fat32/fat32.c b/kernel/filesystem/fat32/fat32.c index 44f2478d..e26a4917 100644 --- a/kernel/filesystem/fat32/fat32.c +++ b/kernel/filesystem/fat32/fat32.c @@ -28,7 +28,7 @@ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t { // 挂载文件系统到vfs - return vfs_mount_fs("FAT32", (ahci_gendisk0.partition + 0)); + return vfs_mount_fs("/", "FAT32", (ahci_gendisk0.partition + 0)); } /** @@ -552,7 +552,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos // find the actual cluster on disk of the specified position for (int i = 0; i < clus_offset_in_file; ++i) - cluster = fat32_read_FAT_entry(blk,fsbi, cluster); + cluster = fat32_read_FAT_entry(blk, fsbi, cluster); // 如果需要读取的数据边界大于文件大小 if (*position + count > file_ptr->dEntry->dir_inode->file_size) @@ -597,7 +597,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos *position += step_trans_len; // 更新文件指针 - cluster = fat32_read_FAT_entry(blk,fsbi, cluster); + cluster = fat32_read_FAT_entry(blk, fsbi, cluster); } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0); kfree(tmp_buffer); @@ -641,7 +641,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po { // 跳转到position所在的簇 for (uint64_t i = 0; i < clus_offset_in_file; ++i) - cluster = fat32_read_FAT_entry(blk,fsbi, cluster); + cluster = fat32_read_FAT_entry(blk, fsbi, cluster); } // kdebug("cluster(start)=%d", cluster); // 没有可用的磁盘空间 @@ -704,7 +704,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po int next_clus = 0; if (bytes_remain) - next_clus = fat32_read_FAT_entry(blk,fsbi, cluster); + next_clus = fat32_read_FAT_entry(blk, fsbi, cluster); else break; if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇 @@ -1069,7 +1069,7 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f // 循环读取fat entry,直到读取到文件当前位置的所在簇号 for (int i = 0; i < clus_num; ++i) { - cluster = fat32_read_FAT_entry(blk,fsbi, cluster); + cluster = fat32_read_FAT_entry(blk, fsbi, cluster); if (cluster > 0x0ffffff7) // 文件结尾 { kerror("file position out of range! (cluster not exists)"); @@ -1263,6 +1263,6 @@ void fat32_init() vfs_register_filesystem(&fat32_fs_type); // 挂载根文件系统 - vfs_root_sb = fat32_register_partition(0, 0, 0); + fat32_register_partition(0, 0, 0); kinfo("FAT32 initialized."); } \ No newline at end of file