《操作系统真象还原》第十四章 文件系统
第十四章 文件系统
本文是对《操作系统真象还原》第十四章学习的笔记,欢迎大家一起交流,目前所有代码已托管至 fdx-xdf/MyTinyOS 。
a 创建文件系统
知识部分
我们文件系统采用的是 inode 进行管理,大致原理如下图:
在 Linux 中,目录和文件都用 inode 来表示,因此目录也是文件,只是目录是包含文件的文件 。
如果该 inode 表示的是普通文件,此 inode 指向的数据块中的内容应该是普通文件自己的数据。如果该 inode 表示的是目录文件,此 inode 指向的数据块中的内容应该是该目录下的目录项
有了目录项后,通过文件名找文件实体数据块的流程是 。
( l )在目录中找到文件名所在的目录项 。
(2 )从目录项中获取 inode 编号 。
(3 )用 inode 编号作为 inode 数组的索引下标,找到 inode
(4 )从该 inode 中获取数据块的地址,读取数据块 。
我们现在知道要找文件先找目录,那去哪里找目录呢,目录的目录?肯定会有的终点,那就是根目录,而根目录的位置又可以在超级块中找到,其结果如下图所示:
超级块的位置也是固定的,在其分区的第二个块上
总结一下:
- inode:
inode 记录了文件与磁盘位置的映射。
每个文件对应一个 inode。当我们找到了这个文件对应的 inode,就能知道这个文件在磁盘中的存储位置。
每个磁盘分区的所有 inode 都会形成一个数组。使用文件的 inode 数组下标,我们可以在该数组中查找对应的 inode 信息。 - 目录:
一个目录其实也是一个特殊的文件。
目录由众多目录项构成,目录项记录了文件名到 inode 的映射。如果一个目录管理/Desktop 文件夹,那么这个目录下的众多目录项就是管理/Desktop 文件夹下的各个文件和子目录。 - 超级块:
超级块包含了关于 inode、目录及其他文件系统元数据的信息。
通常,它位于每个磁盘分区的第二个扇区,这是一个固定位置。
对于 inode,它记录了 inode 数组在磁盘中的起始位置;对于根目录,超级块还会记录其 inode 标号。
如果我们要查找一个/home/test.c,那么文件系统是如何工作的呢?
- 首先,由于超级块位置固定,我们可以去磁盘中直接访问它。
- 从超级块中,我们能知道根目录的 inode 标号和 inode 数组的位置。
- 使用根目录的 inode 标号与 inode 数组位置,我们可以找到根目录文件对应的 inode,然后在磁盘中找到根目录文件。
- 在根目录文件中,我们查找一个名叫 home 的目录项,从中取出 home 的 inode 数组标号。
- 使用 home 的 inode 标号,我们再次访问 inode 数组,找到 home 目录文件在磁盘上的实际位置。
- 最后,在 home 目录文件中,我们查找一个名叫 test.c 的目录项,从中取出 test.c 的 inode 数组标号,进而找到 test.c 在磁盘上的位置。
代码部分
数据结构准备
/fs/super_block.h
超级块
#ifndef __FS_SUPER_BLOCK_H
#define __FS_SUPER_BLOCK_H
#include "stdint.h"
/* 超级块 */
struct super_block {
uint32_t magic; // 用来标识文件系统类型,支持多文件系统的操作系统通过此标志来识别文件系统类型
uint32_t sec_cnt; // 本分区总共的扇区数
uint32_t inode_cnt; // 本分区中inode数量
uint32_t part_lba_base; // 本分区的起始lba地址
uint32_t block_bitmap_lba; // 块位图本身起始扇区地址
uint32_t block_bitmap_sects; // 扇区位图本身占用的扇区数量
uint32_t inode_bitmap_lba; // i结点位图起始扇区lba地址
uint32_t inode_bitmap_sects; // i结点位图占用的扇区数量
uint32_t inode_table_lba; // i结点表起始扇区lba地址
uint32_t inode_table_sects; // i结点表占用的扇区数量
uint32_t data_start_lba; // 数据区开始的第一个扇区号
uint32_t root_inode_no; // 根目录所在的I结点号
uint32_t dir_entry_size; // 目录项大小
uint8_t pad[460]; // 加上460字节,凑够512字节1扇区大小
} __attribute__ ((packed));
#endif
/fs/inode.h
inode 结构
#ifndef __FS_INODE_H
#define __FS_INODE_H
#include "stdint.h"
#include "list.h"
/* inode结构 */
struct inode {
uint32_t i_no; // inode编号
/* 当此inode是文件时,i_size是指文件大小,
若此inode是目录,i_size是指该目录下所有目录项大小之和*/
uint32_t i_size;
uint32_t i_open_cnts; // 记录此文件被打开的次数
bool write_deny; // 写文件不能并行,进程写文件前检查此标识
/* i_sectors[0-11]是直接块, i_sectors[12]用来存储一级间接块指针 */
uint32_t i_sectors[13];
struct list_elem inode_tag;
};
#endif
/fs/dir.h
目录结构和目录项
#ifndef __FS_DIR_H
#define __FS_DIR_H
#include "stdint.h"
#include "inode.h"
#define MAX_FILE_NAME_LEN 16 // 最大文件名长度
/* 目录结构 */
struct dir {
struct inode* inode;
uint32_t dir_pos; // 记录在目录内的偏移
uint8_t dir_buf[512]; // 目录的数据缓存
};
/* 目录项结构 */
struct dir_entry {
char filename[MAX_FILE_NAME_LEN]; // 普通文件或目录名称
uint32_t i_no; // 普通文件或目录对应的inode编号
enum file_types f_type; // 文件类型
};
#endif
/fs/fs.h
#ifndef __FS_FS_H
#define __FS_FS_H
#include "stdint.h"
#define MAX_FILES_PER_PART 4096 // 每个分区所支持最大创建的文件数
#define BITS_PER_SECTOR 4096 // 每扇区的位数
#define SECTOR_SIZE 512 // 扇区字节大小
#define BLOCK_SIZE SECTOR_SIZE // 块字节大小
/* 文件类型 */
enum file_types {
FT_UNKNOWN, // 不支持的文件类型
FT_REGULAR, // 普通文件
FT_DIRECTORY // 目录
};
void filesys_init(void);
#endif
初始化函数
/fs.fs,c
/* 格式化分区,也就是初始化分区的元信息,创建文件系统 */
static void partition_format(struct partition *part)
{
/* 为方便实现,一个块大小是一扇区 */
uint32_t boot_sector_sects = 1;
uint32_t super_block_sects = 1;
uint32_t inode_bitmap_sects = DIV_ROUND_UP(MAX_FILES_PER_PART, BITS_PER_SECTOR); // I结点位图占用的扇区数.最多支持4096个文件
uint32_t inode_table_sects = DIV_ROUND_UP(((sizeof(struct inode) * MAX_FILES_PER_PART)), SECTOR_SIZE);
uint32_t used_sects = boot_sector_sects + super_block_sects + inode_bitmap_sects + inode_table_sects;
uint32_t free_sects = part->sec_cnt - used_sects;
/************** 简单处理块位图占据的扇区数 ***************/
// 不太精确 够用即可
uint32_t block_bitmap_sects;
block_bitmap_sects = DIV_ROUND_UP(free_sects, BITS_PER_SECTOR);
/* block_bitmap_bit_len是位图中位的长度,也是可用块的数量 */
uint32_t block_bitmap_bit_len = free_sects - block_bitmap_sects;
block_bitmap_sects = DIV_ROUND_UP(block_bitmap_bit_len, BITS_PER_SECTOR);
/* 超级块初始化 */
struct super_block sb;
sb.magic = 0x19590318;
sb.sec_cnt = part->sec_cnt;
sb.inode_cnt = MAX_FILES_PER_PART;
sb.part_lba_base = part->start_lba;
sb.block_bitmap_lba = sb.part_lba_base + 2; // 第0块是引导块,第1块是超级块
sb.block_bitmap_sects = block_bitmap_sects;
sb.inode_bitmap_lba = sb.block_bitmap_lba + sb.block_bitmap_sects;
sb.inode_bitmap_sects = inode_bitmap_sects;
sb.inode_table_lba = sb.inode_bitmap_lba + sb.inode_bitmap_sects;
sb.inode_table_sects = inode_table_sects;
sb.data_start_lba = sb.inode_table_lba + sb.inode_table_sects; // 数据区的起始就是inode数组的结束
sb.root_inode_no = 0;
sb.dir_entry_size = sizeof(struct dir_entry);
printk("%s info:\n", part->name);
printk(" magic:0x%x\n part_lba_base:0x%x\n all_sectors:0x%x\n inode_cnt:0x%x\n block_bitmap_lba:0x%x\n block_bitmap_sectors:0x%x\n inode_bitmap_lba:0x%x\n inode_bitmap_sectors:0x%x\n inode_table_lba:0x%x\n inode_table_sectors:0x%x\n data_start_lba:0x%x\n", sb.magic, sb.part_lba_base, sb.sec_cnt, sb.inode_cnt, sb.block_bitmap_lba, sb.block_bitmap_sects, sb.inode_bitmap_lba, sb.inode_bitmap_sects, sb.inode_table_lba, sb.inode_table_sects, sb.data_start_lba);
struct disk *hd = part->my_disk;
/*******************************
* 1 将超级块写入本分区的1扇区 *
******************************/
ide_write(hd, part->start_lba + 1, &sb, 1);
printk(" super_block_lba:0x%x\n", part->start_lba + 1);
/* 找出数据量最大的元信息,用其尺寸做存储缓冲区*/
uint32_t buf_size = (sb.block_bitmap_sects >= sb.inode_bitmap_sects ? sb.block_bitmap_sects : sb.inode_bitmap_sects);
buf_size = (buf_size >= sb.inode_table_sects ? buf_size : sb.inode_table_sects) * SECTOR_SIZE;
uint8_t *buf = (uint8_t *)sys_malloc(buf_size); // 申请的内存由内存管理系统清0后返回
/**************************************
* 2 将块位图初始化并写入sb.block_bitmap_lba *
*************************************/
/* 初始化块位图block_bitmap */
buf[0] |= 0x01; // 第0个块预留给根目录,位图中先占位
uint32_t block_bitmap_last_byte = block_bitmap_bit_len / 8; // 计算出块位图最后一字节的偏移
uint8_t block_bitmap_last_bit = block_bitmap_bit_len % 8; // 计算出块位图最后一字节中有效位的数量
uint32_t last_size = SECTOR_SIZE - (block_bitmap_last_byte % SECTOR_SIZE); // last_size是位图所在最后一个扇区中,不足一扇区的其余部分
/* 1 先将位图最后一字节到其所在的扇区的结束全置为1,即超出实际块数的部分直接置为已占用*/
memset(&buf[block_bitmap_last_byte], 0xff, last_size);
/* 2 再将上一步中覆盖的最后一字节内的有效位重新置0 */
uint8_t bit_idx = 0;
while (bit_idx < block_bitmap_last_bit)
buf[block_bitmap_last_byte] &= ~(1 << bit_idx++);
ide_write(hd, sb.block_bitmap_lba, buf, sb.block_bitmap_sects);
/***************************************
* 3 将inode位图初始化并写入sb.inode_bitmap_lba *
***************************************/
/* 先清空缓冲区*/
memset(buf, 0, buf_size);
buf[0] |= 0x1; // 第0个inode分给了根目录
/* 由于inode_table中共4096个inode,位图inode_bitmap正好占用1扇区,
* 即inode_bitmap_sects等于1, 所以位图中的位全都代表inode_table中的inode,
* 无须再像block_bitmap那样单独处理最后一扇区的剩余部分,
* inode_bitmap所在的扇区中没有多余的无效位 */
ide_write(hd, sb.inode_bitmap_lba, buf, sb.inode_bitmap_sects);
/***************************************
* 4 将inode表初始化并写入sb.inode_table_lba *
***************************************/
/* 准备写inode_table中的第0项,即根目录所在的inode */
memset(buf, 0, buf_size); // 先清空缓冲区buf
struct inode *i = (struct inode *)buf;
i->i_size = sb.dir_entry_size * 2; // 两个目录项 . 和 ..
i->i_no = 0; // 根目录占inode数组中第0个inode
i->i_sectors[0] = sb.data_start_lba;
ide_write(hd, sb.inode_table_lba, buf, sb.inode_table_sects);
/***************************************
* 5 将根目录初始化并写入sb.data_start_lba
***************************************/
/* 写入根目录的两个目录项.和.. */
memset(buf, 0, buf_size);
struct dir_entry *p_de = (struct dir_entry *)buf;
/* 初始化当前目录"." */
memcpy(p_de->filename, ".", 1);
p_de->i_no = 0;
p_de->f_type = FT_DIRECTORY;
p_de++;
/* 初始化当前目录父目录".." */
memcpy(p_de->filename, "..", 2);
p_de->i_no = 0; // 根目录的父目录依然是根目录自己
p_de->f_type = FT_DIRECTORY;
/* sb.data_start_lba已经分配给了根目录,里面是根目录的目录项 */
ide_write(hd, sb.data_start_lba, buf, 1);
printk(" root_dir_lba:0x%x\n", sb.data_start_lba);
printk("%s format done\n", part->name);
sys_free(buf);
}
这个函数是用来格式化分区的,上机运行时会对磁盘进行校对,如果发现还没有格式化就会调用该函数进行格式化,格式化一次过后就不会再用到该函数了。
核心思想就是将超级块、空闲块位图、inode 位图、inode 数组、根目录写到分区对应的位置。
- 4-18 行是先计算出各个部分需要的磁盘空间大小以及在磁盘上的起始地址。
- 20-48 行则是初始化超级块,将超级块写到硬盘上
- 51-53 行找到剩下需要写到硬盘中的数据结构哪个需要的空间最大,然后在堆上申请,防止爆栈
- 55-71 行写空闲块位图,空闲块位图最后一个扇区有一些是用不上的,但是我们要把它置为 1,这样方便以后得应用
- 71-115 行依次写入 inode 位图,inode 数组,根目录
/* 在磁盘上搜索文件系统,若没有则格式化分区创建文件系统 */
void filesys_init()
{
uint8_t channel_no = 0, dev_no, part_idx = 0;
/* sb_buf用来存储从硬盘上读入的超级块 */
struct super_block *sb_buf = (struct super_block *)sys_malloc(SECTOR_SIZE);
if (sb_buf == NULL)
{
PANIC("alloc memory failed!");
}
printk("searching filesystem......\n");
while (channel_no < channel_cnt)
{ // 遍历两个通道
dev_no = 0;
while (dev_no < 2)
{ // 遍历通道下1主1从两个硬盘
if (dev_no == 0)
{ // 跨过裸盘hd60M.img
dev_no++;
continue;
}
struct disk *hd = &channels[channel_no].devices[dev_no];
struct partition *part = hd->prim_parts;
while (part_idx < 12)
{ // 遍历硬盘的分区,4个主分区+8个逻辑
if (part_idx == 4)
{ // 开始处理逻辑分区
part = hd->logic_parts;
}
/* channels数组是全局变量,默认值为0,disk属于其嵌套结构,
* partition又为disk的嵌套结构,因此partition中的成员默认也为0.
* 若partition未初始化,则partition中的成员仍为0.
* 下面处理存在的分区. */
if (part->sec_cnt != 0)
{
// 分区存在
// 读出来超级块
ide_read(hd, part->start_lba + 1, sb_buf, 1);
if (sb_buf->magic == 0x19590318)
printk("%s has filesystem\n", part->name);
else
{ // 其它文件系统不支持,一律按无文件系统处理
printk("formatting %s`s partition %s......\n", hd->name, part->name);
partition_format(part);
}
}
part_idx++;
part++; // 下一分区
}
dev_no++;
}
channel_no++;
}
sys_free(sb_buf);
}
这个函数是文件系统初始化函数,依次遍历通道磁盘分区,然后校验魔数,如果校验通过直接输出成功信息,否则进行格式化,我们在这里硬编码跳过了裸盘 hd60M.img。
结果如下,第一次会输出格式化的信息
第二次运行则直接输出已经有了文件系统
b 挂载分区
之前我们写好了函数用于创建文件系统元数据,并且将其写入了磁盘。当我们实际要用到某个分区的数据,就需要挂载该分区,也就是读取磁盘中文件系统元数据(超级块,块位图,inode 位图,inode 数组由于过大,不读入到内存中,)到内存中,然后对这些元数据的改动,要及时同步到磁盘中。现在我们就来写这个挂载分区的函数 mount_partition。这个函数会被 list_traversal 调用。
/fs/fs.c
struct partition *cur_part; // 默认情况下操作的是哪个分区
/* 在分区链表中找到名为part_name的分区,并将其指针赋值给cur_part */
static bool mount_partition(struct list_elem *pelem, int arg)
{
char *part_name = (char *)arg;
struct partition *part = elem2entry(struct partition, part_tag, pelem);
if (!strcmp(part->name, part_name)){
cur_part = part;
struct disk *hd = cur_part->my_disk;
/* sb_buf用来存储从硬盘上读入的超级块 */
struct super_block *sb_buf = (struct super_block *)sys_malloc(SECTOR_SIZE);
/* 在内存中创建分区cur_part的超级块 */
cur_part->sb = (struct super_block *)sys_malloc(sizeof(struct super_block));
if (cur_part->sb == NULL)
PANIC("alloc memory failed!");
// 读入超级块
memset(sb_buf, 0, SECTOR_SIZE);
ide_read(hd, cur_part->start_lba + 1, sb_buf, 1);
memcpy(cur_part->sb, sb_buf, sizeof(struct super_block));
// 将硬盘上的块位图读入到内存
cur_part->block_bitmap.bits = (uint8_t *)sys_malloc(sb_buf->block_bitmap_sects * SECTOR_SIZE);
if (cur_part->block_bitmap.bits == NULL)
{
PANIC("alloc memory failed!");
}
cur_part->block_bitmap.btmp_bytes_len = sb_buf->block_bitmap_sects * SECTOR_SIZE;
/* 从硬盘上读入块位图到分区的block_bitmap.bits */
ide_read(hd, sb_buf->block_bitmap_lba, cur_part->block_bitmap.bits, sb_buf->block_bitmap_sects);
/*************************************************************/
/********** 将硬盘上的inode位图读入到内存 ************/
cur_part->inode_bitmap.bits = (uint8_t *)sys_malloc(sb_buf->inode_bitmap_sects * SECTOR_SIZE);
if (cur_part->inode_bitmap.bits == NULL)
{
PANIC("alloc memory failed!");
}
cur_part->inode_bitmap.btmp_bytes_len = sb_buf->inode_bitmap_sects * SECTOR_SIZE;
/* 从硬盘上读入inode位图到分区的inode_bitmap.bits */
ide_read(hd, sb_buf->inode_bitmap_lba, cur_part->inode_bitmap.bits, sb_buf->inode_bitmap_sects);
/*************************************************************/
list_init(&cur_part->open_inodes);
printk("mount %s done!\n", part->name);
/* 此处返回true是为了迎合主调函数list_traversal的实现,与函数本身功能无关。
只有返回true时list_traversal才会停止遍历,减少了后面元素无意义的遍历.*/
sys_free(sb_buf);
return true;
}
// 返回false 表示继续找下一个分区
return false;
}
函数很简单,就是不断遍历分区,找到想要的,然后将其超级块加载进来,借助超级块的相关信息加载块位图、inode 位图到内存中。
然后将挂载分区的函数在文件系统初始化的函数中进行调用即可(使用 list_traversal
)。
c 创建文件
文件系统的核心功能是管理文件(inode)与其在磁盘上存储位置之间的映射关系。但当我们谈到实际的文件操作(如打开、读取、写入),这些操作总是基于特定的进程或线程来进行。所以我们需要一个机制来建立进程或线程到文件(inode)的映射,这正是文件描述符的作用。而文件描述符则是基于“文件结构”来实现的。
文件结构详细描述了应用进程对于一个文件的当前操作状态:
- 当应用进程操作(比如打开)一个文件时,操作系统会创建一个对应的全局文件结构,放置于全局文件结构数组(也叫文件表)中,用于跟踪该进程在该文件上的操作状态。
- 文件结构中包含一个 fd_pos 字段,它表示当前进程操作的位置。例如,当我们打开一个文件 test.txt,fd_pos 的初始值通常是文件的开头。
- 当我们对文件执行实际的读写操作,fd_pos 会被更新到操作的位置。例如,如果在文件中间开始写入数据,fd_pos 会更新为那个位置并随着写入操作继续前移。当进行编辑操作时,输入的内容首先被存放在一个内存输入缓冲区中。按下 ctrl + s 后,操作系统会使用文件结构中的 fd_pos 作为起始点,将缓冲区的内容写入磁盘文件中,同时更新 fd_pos 到写入结束后的位置。
- 文件结构还包含一个字段 fd_inode,它是对应文件的 inode 的指针。通过这个 fd_inode 字段,操作系统可以轻松地找到文件在磁盘上的位置,并将内存输入缓冲区的内容写入磁盘。
由于文件结构与具体的应用程序高度相关,我们需要引入一个新的概念:文件描述符数组。
文件描述符数组是存储在进程的进程控制块(PCB)中的一个数组。每个元素都是一个文件描述符(就是一个数字),它指向全局文件结构数组中的一个特定条目。
当进程打开一个文件,操作系统会为这个文件创建一个文件结构并将其放入全局文件结构数组中。接着,操作系统会将这个文件结构在全局数组中的位置(或索引)赋给进程的文件描述符数组中的一个空闲元素。
例如一个进程打开了两个文件,它们在全局文件结构数组中的位置分别是 32 和 58。那么,进程的 PCB 中的文件描述符数组的第 0 个元素就会被赋值为 32,而第 1 个元素则会被赋值为 58。
PCB 中的文件描述符数组的大小限制了一个进程能够同时打开文件的最大数量。如果进程打开一个新的文件,但文件描述符数组已满,则操作将失败。
需要注意的是,打开不同的文件当然会消耗一个文件描述符。但由于我们在文件结构中引入了文件操作位置的概念(即 fd_pos),如果一个进程多次打开同一个文件(即每次打开都有自己的读写位置),那么每次打开都会消耗一个新的文件描述符。
上图是 Linux 通过文件描述符查找文件数据块的过程,这涉及到以下三个数据结构,它们都是位于内存中的。
(1)PCB 中的文件描述符数组。
(2)存储所有文件结构的文件表。
(3)inode 队列,也就是 inode 缓存。
现在从左到右上图,某进程把文件描述符作为参数提交给文件系统时,文件系统用此文件描述符在该进程的 PCB 中的文件描述符数组中索引对应的元素,从该元素中获取对应的文件结构的下标,用该下标在文件表中索引相应的文件结构,从该文件结构中获取文件的 inode,最终找到了文件的数据块。提示一下,若该 inode 在 inode 队列中不存在,此时会多一个处理过程:文件系统会从硬盘上将该 inode 加载到 inode 队列中,并使文件结构中的 fd_inode 指向它,将来介绍 inode 操作相关的函数时会再次提到。
文件系统这一章函数,代码非常多,其实核心就是处理超级块,inode,目录,文件结构这四个概念之间的交互。
文件描述符实现
在 pcb 里面加入 fd_table
,也就是用户的文件打开表,然后将其在 init_thread 中初始化,除了前三个表项预留之外,其他的都初始化为-1。
#define MAX_FILES_OPEN_PER_PROC 8
/* 进程或线程的pcb,程序控制块, 此结构体用于存储线程的管理信息*/
struct task_struct
{
uint32_t *self_kstack; // 用于存储线程的栈顶位置,栈顶放着线程要用到的运行信息
pid_t pid;
enum task_status status;
uint8_t priority; // 线程优先级
char name[16]; // 用于存储自己的线程的名字
uint8_t ticks; // 线程允许上处理器运行还剩下的滴答值,因为priority不能改变,所以要在其之外另行定义一个值来倒计时
uint32_t elapsed_ticks; // 此任务自上cpu运行后至今占用了多少cpu嘀嗒数, 也就是此任务执行了多久*/
struct list_elem general_tag; // general_tag的作用是用于线程在一般的队列(如就绪队列或者等待队列)中的结点
struct list_elem all_list_tag; // all_list_tag的作用是用于线程队列thread_all_list(这个队列用于管理所有线程)中的结点
uint32_t *pgdir; // 进程自己页表的虚拟地址
struct virtual_addr userprog_vaddr; // 用户进程的虚拟内存池
int32_t fd_table[MAX_FILES_OPEN_PER_PROC]; // 已打开文件数组
struct mem_block_desc u_block_desc[DESC_CNT]; // 用户进程内存块描述符
uint32_t stack_magic; // 如果线程的栈无限生长,总会覆盖地pcb的信息,那么需要定义个边界数来检测是否栈已经到了PCB的边界
};
/* 初始化线程基本信息 , pcb中存储的是线程的管理信息,此函数用于根据传入的pcb的地址,线程的名字等来初始化线程的管理信息*/
void init_thread(struct task_struct *pthread, char *name, int prio)
{
......
......
// 预留标准输入输出
pthread->fd_table[0] = 0;
pthread->fd_table[1] = 1;
pthread->fd_table[2] = 2;
/* 其余的全置为-1 */
uint8_t fd_idx = 3;
while (fd_idx < MAX_FILES_OPEN_PER_PROC)
{
pthread->fd_table[fd_idx] = -1;
fd_idx++;
}
pthread->stack_magic = 0x19870916; // /定义的边界数字,随便选的数字来判断线程的栈是否已经生长到覆盖pcb信息了
}
文件操作基本函数
inode 操作
任何有关文件及目录的操作都了不开对 inode 的操作,因为我们需要通过 inode 知道文件的存储位置,所以操作文件,总是意味着要找到该文件的 inode。接下来我们实现一堆对于 inode 的处理函数。涉及:找到一个 inode 在磁盘中的位置,初始化一个 inode,加载该 inode 到内存中,修改内存中的 inode 之后同步到磁盘中,从内存中删除一个 inode。
/fs/inode.c
首先先定义一个结构体方便我们操作
/* 用来存储inode位置 */
struct inode_position
{
bool two_sec; // inode是否跨扇区
uint32_t sec_lba; // inode所在的扇区号
uint32_t off_size; // inode在扇区内的字节偏移量
};
inode_locate
获取 inode 所在的扇区和扇区内的偏移量
static void inode_locate(struct partition *part, uint32_t inode_no, struct inode_position *inode_pos)
{
/* inode_table在硬盘上是连续的 */
ASSERT(inode_no < 4096);
uint32_t inode_table_lba = part->sb->inode_table_lba;
uint32_t inode_size = sizeof(struct inode);
uint32_t off_size = inode_no * inode_size; // 第inode_no号I结点相对于inode_table_lba的字节偏移量
uint32_t off_sec = off_size / 512; // 第inode_no号I结点相对于inode_table_lba的扇区偏移量
uint32_t off_size_in_sec = off_size % 512; // 待查找的inode所在扇区中的起始地址
/* 判断此i结点是否跨越2个扇区 */
uint32_t left_in_sec = 512 - off_size_in_sec;
if (left_in_sec < inode_size)
inode_pos->two_sec = true;
else
inode_pos->two_sec = false;
inode_pos->sec_lba = inode_table_lba + off_sec;
inode_pos->off_size = off_size_in_sec;
}
注意就是对于是否跨越扇区的判断
inode_sync
将 inode 写入到分区 part
/* 将inode写入到分区part */
void inode_sync(struct partition *part, struct inode *inode, void *io_buf){
uint8_t inode_no = inode->i_no;
struct inode_position inode_pos;
inode_locate(part, inode_no, &inode_pos); // inode位置信息会存入inode_pos
ASSERT(inode_pos.sec_lba <= (part->start_lba + part->sec_cnt));
/* 硬盘中的inode中的成员inode_tag和i_open_cnts是不需要的,
* 它们只在内存中记录链表位置和被多少进程共享 */
struct inode pure_inode;
memcpy(&pure_inode, inode, sizeof(struct inode));
/* 以下inode的三个成员只存在于内存中,现在将inode同步到硬盘,清掉这三项即可 */
pure_inode.i_open_cnts = 0;
pure_inode.write_deny = false; // 置为false,以保证在硬盘中读出时为可写
pure_inode.inode_tag.prev = pure_inode.inode_tag.next = NULL;
char *inode_buf = (char *)io_buf;
if (inode_pos.two_sec)
{ // 若是跨了两个扇区,就要读出两个扇区再写入两个扇区
/* 读写硬盘是以扇区为单位,若写入的数据小于一扇区,要将原硬盘上的内容先读出来再和新数据拼成一扇区后再写入 */
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 2); // inode在format中写入硬盘时是连续写入的,所以读入2块扇区
/* 开始将待写入的inode拼入到这2个扇区中的相应位置 */
memcpy((inode_buf + inode_pos.off_size), &pure_inode, sizeof(struct inode));
/* 将拼接好的数据再写入磁盘 */
ide_write(part->my_disk, inode_pos.sec_lba, inode_buf, 2);
}
else
{ // 若只是一个扇区
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 1);
memcpy((inode_buf + inode_pos.off_size), &pure_inode, sizeof(struct inode));
ide_write(part->my_disk, inode_pos.sec_lba, inode_buf, 1);
}
}
先调用 inode_locate 获取 inode 磁盘位置,写到对应磁盘位置即可,注意 inode 有一些成员只有在内存中才有意义,所以我们要单独定义一个 pure_inode 进行处理。
inode_open
根据 i 结点号返回相应的 i 结点
/* 根据i结点号返回相应的i结点 */
struct inode *inode_open(struct partition *part, uint32_t inode_no){
/* 先在已打开inode链表中找inode,此链表是为提速创建的缓冲区 */
struct list_elem *elem = part->open_inodes.head.next;
struct inode *inode_found;
while (elem != &part->open_inodes.tail)
{
inode_found = elem2entry(struct inode, inode_tag, elem);
if (inode_found->i_no == inode_no)
{
inode_found->i_open_cnts++;
return inode_found;
}
elem = elem->next;
}
/*由于open_inodes链表中找不到,下面从硬盘上读入此inode并加入到此链表 */
struct inode_position inode_pos;
/* inode位置信息会存入inode_pos, 包括inode所在扇区地址和扇区内的字节偏移量 */
inode_locate(part, inode_no, &inode_pos);
/* 为使通过sys_malloc创建的新inode被所有任务共享,
* 需要将inode置于内核空间,故需要临时
* 将cur_pbc->pgdir置为NULL */
struct task_struct *cur = running_thread();
uint32_t *cur_pagedir_bak = cur->pgdir;
cur->pgdir = NULL;
/* 以上三行代码完成后下面分配的内存将位于内核区 */
inode_found = (struct inode *)sys_malloc(sizeof(struct inode));
/* 恢复pgdir */
cur->pgdir = cur_pagedir_bak;
char *inode_buf;
if (inode_pos.two_sec)
{ // 考虑跨扇区的情况
inode_buf = (char *)sys_malloc(1024);
/* i结点表是被partition_format函数连续写入扇区的,
* 所以下面可以连续读出来 */
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 2);
}
else
{ // 否则,所查找的inode未跨扇区,一个扇区大小的缓冲区足够
inode_buf = (char *)sys_malloc(512);
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 1);
}
memcpy(inode_found, inode_buf + inode_pos.off_size, sizeof(struct inode));
/* 因为一会很可能要用到此inode,故将其插入到队首便于提前检索到 */
list_push(&part->open_inodes, &inode_found->inode_tag);
inode_found->i_open_cnts = 1;
sys_free(inode_buf);
return inode_found;
}
首先我们要现在 inode 缓存列表中查找,如果找到了直接返回,如果没找到,就在去硬盘上找,并且加入到此链表中
在链表中没有找到时,我们要在内核空间中去申请 inode 内存,然后加入到链表中,但是我们现在是用户进程调用的,怎么办?我们这里并没有去改 cr3 里面的值,作者给出的做法是将 cur->pgdir = NULL,此时去 sys_malloc 的时候就会在内核空间申请,然后根据 inode_locate 的定位信息去磁盘中找,找到之后复制到内存中,然后给加入到链表队首即可
inode_close
关闭 inode 或减少 inode 的打开数
void inode_close(struct inode *inode){
/* 若没有进程再打开此文件,将此inode去掉并释放空间 */
enum intr_status old_status = intr_disable();
if (--inode->i_open_cnts == 0)
{
list_remove(&inode->inode_tag); // 将I结点从part->open_inodes中去掉
/* inode_open时为实现inode被所有进程共享,
* 已经在sys_malloc为inode分配了内核空间,
* 释放inode时也要确保释放的是内核内存池 */
struct task_struct *cur = running_thread();
uint32_t *cur_pagedir_bak = cur->pgdir;
cur->pgdir = NULL;
sys_free(inode);
cur->pgdir = cur_pagedir_bak;
}
intr_set_status(old_status);
}
函数很简单,不再解释
inode_init
初始化 new_inode
void inode_init(uint32_t inode_no, struct inode *new_inode)
{
new_inode->i_no = inode_no;
new_inode->i_size = 0;
new_inode->i_open_cnts = 0;
new_inode->write_deny = false;
/* 初始化块索引数组i_sector */
uint8_t sec_idx = 0;
while (sec_idx < 13)
{
/* i_sectors[12]为一级间接块地址 */
new_inode->i_sectors[sec_idx] = 0;
sec_idx++;
}
}
初始化一个新的 inode,创建文件时用
文件操作
/fs/file.h
#define MAX_FILE_OPEN 32 // 系统可打开的最大文件数
/* 文件结构 */
struct file {
uint32_t fd_pos; // 记录当前文件操作的偏移地址,以0为起始,最大为文件大小-1
uint32_t fd_flag;
struct inode* fd_inode;
};
/* 标准输入输出描述符 */
enum std_fd {
stdin_no, // 0 标准输入
stdout_no, // 1 标准输出
stderr_no // 2 标准错误
};
/* 位图类型 */
enum bitmap_type {
INODE_BITMAP, // inode位图
BLOCK_BITMAP // 块位图
};
先定义了系统可打开的最大文件数,32
然后是文件结构,fd_pos 是文件操作偏移地址,fd_flag 是文件标识,比如 readonly,fd_inode 指向 inode 队列中的 inode
然后是三个标准输入输出符
然后是位图类型,可以简化函数操作
/fs/file.c
先是声明全局文件表
/* 文件表 */
struct file file_table[MAX_FILE_OPEN];
get_free_slot_in_global
从文件表 file_table 中获取一个空闲位
/* 从文件表file_table中获取一个空闲位,成功返回下标,失败返回-1 */
int32_t get_free_slot_in_global(void){
uint32_t fd_idx = 3;
while (fd_idx < MAX_FILE_OPEN)
{
if (file_table[fd_idx].fd_inode == NULL)
{
break;
}
fd_idx++;
}
if (fd_idx == MAX_FILE_OPEN)
{
printk("exceed max open files\n");
return -1;
}
return fd_idx;
}
就是单纯的遍历文件表,很简单
pcb_fd_install
将全局文件表的索引增加到 pcb 对应的打开文件表中
/* 将全局描述符下标安装到进程或线程自己的文件描述符数组fd_table中,
* 成功返回下标,失败返回-1 */
int32_t pcb_fd_install(int32_t globa_fd_idx)
{
struct task_struct *cur = running_thread();
uint8_t local_fd_idx = 3; // 跨过stdin,stdout,stderr
while (local_fd_idx < MAX_FILES_OPEN_PER_PROC)
{
if (cur->fd_table[local_fd_idx] == -1)
{ // -1表示free_slot,可用
cur->fd_table[local_fd_idx] = globa_fd_idx;
break;
}
local_fd_idx++;
}
if (local_fd_idx == MAX_FILES_OPEN_PER_PROC)
{
printk("exceed max open files_per_proc\n");
return -1;
}
return local_fd_idx;
}
就是将将全局文件表的索引增加到 pcb 对应的打开文件表中,这样进程也能找到了,具体实现也很简单
*_bitmap_alloc
分配一个 i 结点/扇区,返回 i 结点号/扇区地址
/* 分配一个i结点,返回i结点号 */
int32_t inode_bitmap_alloc(struct partition *part)
{
int32_t bit_idx = bitmap_scan(&part->inode_bitmap, 1);
if (bit_idx == -1)
{
return -1;
}
bitmap_set(&part->inode_bitmap, bit_idx, 1);
return bit_idx;
}
/* 分配1个扇区,返回其扇区地址 */
int32_t block_bitmap_alloc(struct partition *part)
{
int32_t bit_idx = bitmap_scan(&part->block_bitmap, 1);
if (bit_idx == -1)
{
return -1;
}
bitmap_set(&part->block_bitmap, bit_idx, 1);
/* 和inode_bitmap_malloc不同,此处返回的不是位图索引,而是具体可用的扇区地址 */
return (part->sb->data_start_lba + bit_idx);
}
具体实现也很简单,但是 inode 返回的是位图索引(也就是 inode 节点号),block 返回的是扇区地址
bitmap_sync
将内存中 bitmap 第 bit_idx 位所在的 512 字节同步到硬盘
/* 将内存中bitmap第bit_idx位所在的512字节同步到硬盘 */
void bitmap_sync(struct partition *part, uint32_t bit_idx, uint8_t btmp_type){
uint32_t off_sec = bit_idx / 4096; // 本i结点索引相对于位图的扇区偏移量
uint32_t off_size = off_sec * BLOCK_SIZE; // 本i结点索引相对于位图的字节偏移量
uint32_t sec_lba;
uint8_t *bitmap_off;
/* 需要被同步到硬盘的位图只有inode_bitmap和block_bitmap */
switch (btmp_type)
{
case INODE_BITMAP:
sec_lba = part->sb->inode_bitmap_lba + off_sec;
bitmap_off = part->inode_bitmap.bits + off_size;
break;
case BLOCK_BITMAP:
sec_lba = part->sb->block_bitmap_lba + off_sec;
bitmap_off = part->block_bitmap.bits + off_size;
break;
}
ide_write(part->my_disk, sec_lba, bitmap_off, 1);
}
注意,这里同步的是位图本身的数据,而不是位图所指向的空间的数据
目录相关
接下来实现一堆与目录相关的函数,涉及目录打开、关闭,在一个目录文件中寻找指定目录项,初始化一个目录项,将目录项写入父目录中。
/fs/dir.c
open_root_dir dir_open
/* 打开根目录 */
void open_root_dir(struct partition *part)
{
root_dir.inode = inode_open(part, part->sb->root_inode_no);
root_dir.dir_pos = 0;
}
/* 在分区part上打开i结点为inode_no的目录并返回目录指针 */
struct dir *dir_open(struct partition *part, uint32_t inode_no)
{
struct dir *pdir = (struct dir *)sys_malloc(sizeof(struct dir));
pdir->inode = inode_open(part, inode_no);
pdir->dir_pos = 0;
return pdir;
}
open_root_dir
用于根据传入的分区,调用 inode_open
将根目录文件的 inode 调入内存,并将全局变量 struct root_dir
中的 inode 指针,指向根目录文件的 inode
dir_open 用于根据传入的分区与 inode 偏移,申请一块内存区域存放目录,调用 inode_open 找到这个目录对应的 inode 并载入内存,然后让目录中的 inode 指针指向这个 inode。功能和 open_root_dir 类似,只不过一个是直接修改全局变量,一个要自行申请内存。
所以我们可以发现,打开目录,就是建立目录(struct dir 结构体)与 inode 之间的关系。为什么?因为目录是一个内存中的概念,但它本身是磁盘中的一个文件!为了建立从内存到磁盘的关系,所以我们需要建立 struct dir 与 inode 之间的关系。所以我们打开目录,自然意味着这个 struct dir 结构体要在内存中。再进一步思考,我们打开目录,肯定意味着要查找这个目录下什么东西(就像是你打开文件夹,是为了找到文件夹下某个文件),而只有 inode 记录着这个目录文件在磁盘中的位置,而目录文件就是一大堆目录项的集合,这些目录项记录着这个目录有啥。所以为了找到目录下有啥,我们肯定要找到它的 inode,调入内存,并建立二者之间的关系。
search_dir_entry
在 part 分区内的 pdir 目录内寻找名为 name 的文件或目录
/* 在part分区内的pdir目录内寻找名为name的文件或目录,
* 找到后返回true并将其目录项存入dir_e,否则返回false */
bool search_dir_entry(struct partition *part, struct dir *pdir, const char *name, struct dir_entry *dir_e)
{
uint32_t block_cnt = 140; // 12个直接块+128个一级间接块=140块
/* 12个直接块大小+128个间接块,共560字节 */
uint32_t *all_blocks = (uint32_t *)sys_malloc(48 + 512);
if (all_blocks == NULL)
{
printk("search_dir_entry: sys_malloc for all_blocks failed");
return false;
}
uint32_t block_idx = 0;
while (block_idx < 12)
{
all_blocks[block_idx] = pdir->inode->i_sectors[block_idx];
block_idx++;
}
block_idx = 0;
if (pdir->inode->i_sectors[12] != 0)
{ // 若含有一级间接块表
ide_read(part->my_disk, pdir->inode->i_sectors[12], all_blocks + 12, 1);
}
/* 至此,all_blocks存储的是该文件或目录的所有扇区地址 */
/* 写目录项的时候已保证目录项不跨扇区,
* 这样读目录项时容易处理, 只申请容纳1个扇区的内存 */
uint8_t *buf = (uint8_t *)sys_malloc(SECTOR_SIZE);
struct dir_entry *p_de = (struct dir_entry *)buf; // p_de为指向目录项的指针,值为buf起始地址
uint32_t dir_entry_size = part->sb->dir_entry_size;
uint32_t dir_entry_cnt = SECTOR_SIZE / dir_entry_size; // 1扇区内可容纳的目录项个数
/* 开始在所有块中查找目录项 */
while (block_idx < block_cnt)
{
/* 块地址为0时表示该块中无数据,继续在其它块中找 */
if (all_blocks[block_idx] == 0)
{
block_idx++;
continue;
}
ide_read(part->my_disk, all_blocks[block_idx], buf, 1);
uint32_t dir_entry_idx = 0;
/* 遍历扇区中所有目录项 */
while (dir_entry_idx < dir_entry_cnt){
/* 若找到了,就直接复制整个目录项 */
if (!strcmp(p_de->filename, name))
{
memcpy(dir_e, p_de, dir_entry_size);
sys_free(buf);
sys_free(all_blocks);
return true;
}
dir_entry_idx++;
p_de++;
}
block_idx++;
p_de = (struct dir_entry *)buf; // 此时p_de已经指向扇区内最后一个完整目录项了,需要恢复p_de指向为buf
memset(buf, 0, SECTOR_SIZE); // 将buf清0,下次再用
}
sys_free(buf);
sys_free(all_blocks);
return false;
}
先为 all_blocks
申请 (12+128)*4
的内存空间,这样可以表示目录所占的所有磁盘地址
我们每个目录项的大小是确定的,并且不跨磁盘写,所以每个磁盘能写多少目录项是固定的,接下来按照这个值挨个块挨个目录项遍历即可
dir_close
关闭目录
/* 关闭目录 */
void dir_close(struct dir *dir)
{
/************* 根目录不能关闭 ***************
*1 根目录自打开后就不应该关闭,否则还需要再次open_root_dir();
*2 root_dir所在的内存是低端1M之内,并非在堆中,free会出问题 */
if (dir == &root_dir)
{
/* 不做任何处理直接返回*/
return;
}
inode_close(dir->inode);
sys_free(dir);
}
dir_close 用于关闭目录,实质就是调用 inode_close
从内存中释放该目录的 inode 占用的内存,并且释放目录占用的内存,也就是解绑 struct dir 与 inode 之间的关系。需要注意的是:根目录不能被释放,因为一定会反复被用到
create_dir_entry
用于初始化一个目录项,也就是给目录项指向的文件一个名字与 inode 索引
/* 在内存中初始化目录项p_de */
void create_dir_entry(char *filename, uint32_t inode_no, uint8_t file_type, struct dir_entry *p_de)
{
ASSERT(strlen(filename) <= MAX_FILE_NAME_LEN);
/* 初始化目录项 */
memcpy(p_de->filename, filename, strlen(filename));
p_de->i_no = inode_no;
p_de->f_type = file_type;
}
sync_dir_entry
将目录项 p_de 写入父目录 parent_dir 中
/* 将目录项p_de写入父目录parent_dir中,io_buf由主调函数提供 */
bool sync_dir_entry(struct dir *parent_dir, struct dir_entry *p_de, void *io_buf)
{
struct inode *dir_inode = parent_dir->inode;
uint32_t dir_size = dir_inode->i_size;
uint32_t dir_entry_size = cur_part->sb->dir_entry_size;
ASSERT(dir_size % dir_entry_size == 0); // dir_size应该是dir_entry_size的整数倍
uint32_t dir_entrys_per_sec = (512 / dir_entry_size); // 每扇区最大的目录项数目
int32_t block_lba = -1;
/* 将该目录的所有扇区地址(12个直接块+ 128个间接块)存入all_blocks */
uint8_t block_idx = 0;
uint32_t all_blocks[140] = {0}; // all_blocks保存目录所有的块
/* 将12个直接块存入all_blocks */
while (block_idx < 12) {
all_blocks[block_idx] = dir_inode->i_sectors[block_idx];
block_idx++;
}
block_idx = 0;
if (dir_inode->i_sectors[12] != 0) { // 若含有一级间接块表
ide_read(cur_part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
}
struct dir_entry *dir_e = (struct dir_entry *)io_buf; // dir_e用来在io_buf中遍历目录项
int32_t block_bitmap_idx = -1;
/* 开始遍历所有块以寻找目录项空位,若已有扇区中没有空闲位,
* 在不超过文件大小的情况下申请新扇区来存储新目录项 */
block_idx = 0;
while (block_idx < 140)
{ // 文件(包括目录)最大支持12个直接块+128个间接块=140个块
block_bitmap_idx = -1;
if (all_blocks[block_idx] == 0)
{ // 在三种情况下分配块
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("alloc block bitmap for sync_dir_entry failed\n");
return false;
}
/* 每分配一个块就同步一次block_bitmap */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
ASSERT(block_bitmap_idx != -1);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
block_bitmap_idx = -1;
if (block_idx < 12)
{ // 若是直接块
dir_inode->i_sectors[block_idx] = all_blocks[block_idx] = block_lba;
}
else if (block_idx == 12)
{ // 若是尚未分配一级间接块表(block_idx等于12表示第0个间接块地址为0)
dir_inode->i_sectors[12] = block_lba; // 将上面分配的块做为一级间接块表地址
block_lba = -1;
block_lba = block_bitmap_alloc(cur_part); // 再分配一个块做为第0个间接块
if (block_lba == -1)
{
block_bitmap_idx = dir_inode->i_sectors[12] - cur_part->sb->data_start_lba;
bitmap_set(&cur_part->block_bitmap, block_bitmap_idx, 0);
dir_inode->i_sectors[12] = 0;
printk("alloc block bitmap for sync_dir_entry failed\n");
return false;
}
/* 每分配一个块就同步一次block_bitmap */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
ASSERT(block_bitmap_idx != -1);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
all_blocks[12] = block_lba;
/* 把新分配的第0个间接块地址写入一级间接块表 */
ide_write(cur_part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
}
else
{ // 若是间接块未分配
all_blocks[block_idx] = block_lba;
/* 把新分配的第(block_idx-12)个间接块地址写入一级间接块表 */
ide_write(cur_part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
}
/* 再将新目录项p_de写入新分配的间接块 */
memset(io_buf, 0, 512);
memcpy(io_buf, p_de, dir_entry_size);
ide_write(cur_part->my_disk, all_blocks[block_idx], io_buf, 1);
dir_inode->i_size += dir_entry_size;
return true;
}
/* 若第block_idx块已存在,将其读进内存,然后在该块中查找空目录项 */
ide_read(cur_part->my_disk, all_blocks[block_idx], io_buf, 1);
/* 在扇区内查找空目录项 */
uint8_t dir_entry_idx = 0;
while (dir_entry_idx < dir_entrys_per_sec)
{
if ((dir_e + dir_entry_idx)->f_type == FT_UNKNOWN)
{ // FT_UNKNOWN为0,无论是初始化或是删除文件后,都会将f_type置为FT_UNKNOWN.
memcpy(dir_e + dir_entry_idx, p_de, dir_entry_size);
ide_write(cur_part->my_disk, all_blocks[block_idx], io_buf, 1);
dir_inode->i_size += dir_entry_size;
return true;
}
dir_entry_idx++;
}
block_idx++;
}
printk("directory is full!\n");
return false;
}
sync_dir_entry 用于将目录项写入父目录中。核心原理就是:父目录结构体 struct dir 有一个指向自己 inode 的成员,该成员记录了该父目录文件在磁盘中存储的位置(inode 的 i_sectors[ ])我们可以根据这个找到目录文件,然后遍历目录文件找到空位,然后将目录项插入到这个空位即可。有些麻烦的是,需要判断 inode 的 i_sectors[ ]这些元素是否为空,如果为空,那么我们还需要申请块用于承载目录文件。
注意,我们这里还要往磁盘里新分配的第(block_idx-12)个间接块地址写入一级间接块表,而其他的直接表或者一级间接块表的地址则不需要,在 inode 进行同步的时候会再写
路径解析
path_parse
path_depth_cnt
/* 将最上层路径名称解析出来 */
static char *path_parse(char *pathname, char *name_store)
{
if (pathname[0] == '/')
{ // 根目录不需要单独解析
/* 路径中出现1个或多个连续的字符'/',将这些'/'跳过,如"///a/b" */
while (*(++pathname) == '/')
;
}
/* 开始一般的路径解析 */
while (*pathname != '/' && *pathname != 0)
*name_store++ = *pathname++;
if (pathname[0] == 0) // 若路径字符串为空则返回NULL
return NULL;
return pathname;
}
/* 返回路径深度,比如/a/b/c,深度为3 */
int32_t path_depth_cnt(char *pathname){
ASSERT(pathname != NULL);
char *p = pathname;
char name[MAX_FILE_NAME_LEN]; // 用于path_parse的参数做路径解析
uint32_t depth = 0;
/* 解析路径,从中拆分出各级名称 */
p = path_parse(p, name);
while (name[0])
{
depth++;
memset(name, 0, MAX_FILE_NAME_LEN);
if (p)
{ // 如果p不等于NULL,继续分析路径
p = path_parse(p, name);
}
}
return depth;
}
path_parse
每次调用就会解析最上层路径名,并且在缓冲区 name_store 中存储解析的路径名,由于根目录是所有路径的最上层,所以不用参与解析(如 /home/test.c
目录层次是/,home,test.c,但是/是所有目录最上层,我们可以忽略,所以 /home/test.c
目录层次就可以认为是 home,test.c)。比如此函数解析 /home/aa/Desktop/test.c
,第一次调用返回 /aa/Desktop/test.c
,name_store 中存储 home。再次调用返回 /Desktop/test.c
,name_store 中存储 aa…
path_depth_cnt
用于返回路径深度,比如 /home/kanshan/Desktop/test.c
路径深度就是 4。原理就是循环调用 path_parse
,看看返回值是不是空,决定是否继续调用 path_parse
文件检索
search_file
搜索文件 pathname,若找到则返回其 inode 号,否则返回-1
/* 搜索文件pathname,若找到则返回其inode号,否则返回-1 */
static int search_file(const char *pathname, struct path_search_record *searched_record)
{
/* 如果待查找的是根目录,为避免下面无用的查找,直接返回已知根目录信息 */
if (!strcmp(pathname, "/") || !strcmp(pathname, "/.") || !strcmp(pathname, "/.."))
{
searched_record->parent_dir = &root_dir;
searched_record->file_type = FT_DIRECTORY;
searched_record->searched_path[0] = 0; // 搜索路径置空
return 0;
}
uint32_t path_len = strlen(pathname);
/* 保证pathname至少是这样的路径/x且小于最大长度 */
ASSERT(pathname[0] == '/' && path_len > 1 && path_len < MAX_PATH_LEN);
char *sub_path = (char *)pathname;
struct dir *parent_dir = &root_dir;
struct dir_entry dir_e;
/* 记录路径解析出来的各级名称,如路径"/a/b/c",
* 数组name每次的值分别是"a","b","c" */
char name[MAX_FILE_NAME_LEN] = {0};
searched_record->parent_dir = parent_dir;
searched_record->file_type = FT_UNKNOWN;
uint32_t parent_inode_no = 0; // 父目录的inode号
sub_path = path_parse(sub_path, name);
while (name[0])
{
// 若第一个字符就是结束符,结束循环
/* 记录查找过的路径,但不能超过searched_path的长度512字节 */
ASSERT(strlen(searched_record->searched_path) < 512);
/* 记录已存在的父目录 */
strcat(searched_record->searched_path, "/");
strcat(searched_record->searched_path, name);
/* 在所给的目录中查找文件 */
if (search_dir_entry(cur_part, parent_dir, name, &dir_e)){
memset(name, 0, MAX_FILE_NAME_LEN);
/* 若sub_path不等于NULL,也就是未结束时继续拆分路径 */
if (sub_path)
sub_path = path_parse(sub_path, name);
if (FT_DIRECTORY == dir_e.f_type)
{ // 如果被打开的是目录
parent_inode_no = parent_dir->inode->i_no;
dir_close(parent_dir);
parent_dir = dir_open(cur_part, dir_e.i_no); // 更新父目录
searched_record->parent_dir = parent_dir;
continue;
}
else if (FT_REGULAR == dir_e.f_type)
{ // 若是普通文件
searched_record->file_type = FT_REGULAR;
return dir_e.i_no;
}
}
else
{ // 若找不到,则返回-1
/* 找不到目录项时,要留着parent_dir不要关闭,
* 若是创建新文件的话需要在parent_dir中创建 */
return -1;
}
}
/* 执行到此,必然是遍历了完整路径并且查找的文件或目录只有同名目录存在 */
dir_close(searched_record->parent_dir);
/* 保存被查找目录的直接父目录 */
searched_record->parent_dir = dir_open(cur_part, parent_inode_no);
searched_record->file_type = FT_DIRECTORY;
return dir_e.i_no;
}
search_file 用于提供文件路径,然后返回 inode 索引。核心原理就是不断循环:1,调用 path_parse 进行地址解析得到除根目录外最上层目录的名字;2,然后调用 search_dir_entry 从搜索目录中(第一次调用这个函数,其搜索目录自然是根目录)查找 1 得到的名字;3,然后更新下一次的搜索目录(就是 2 查找出来的目录项);继续解析,然后继续查找,继续更新搜索目录…
文件创建
file_create
创建文件,流程:创建文件 i 节点——> 文件描述符 fd——> 目录项
/* 创建文件,若成功则返回文件描述符,否则返回-1 */
int32_t file_create(struct dir *parent_dir, char *filename, uint8_t flag)
{
/* 后续操作的公共缓冲区 */
void *io_buf = sys_malloc(1024);
if (io_buf == NULL)
{
printk("in file_creat: sys_malloc for io_buf failed\n");
return -1;
}
uint8_t rollback_step = 0; // 用于操作失败时回滚各资源状态
/* 为新文件分配inode */
int32_t inode_no = inode_bitmap_alloc(cur_part);
if (inode_no == -1)
{
printk("in file_creat: allocate inode failed\n");
goto rollback;
}
/* 此inode要从堆中申请内存,不可生成局部变量(函数退出时会释放)
* 因为file_table数组中的文件描述符的inode指针要指向它.*/
struct inode *new_file_inode = (struct inode *)sys_malloc(sizeof(struct inode));
if (new_file_inode == NULL)
{
printk("file_create: sys_malloc for inode failded\n");
rollback_step = 1;
goto rollback;
}
inode_init(inode_no, new_file_inode); // 初始化i结点
/* 返回的是file_table数组的下标 */
int fd_idx = get_free_slot_in_global();
if (fd_idx == -1)
{
printk("exceed max open files\n");
rollback_step = 2;
goto rollback;
}
file_table[fd_idx].fd_inode = new_file_inode;
file_table[fd_idx].fd_pos = 0;
file_table[fd_idx].fd_flag = flag;
file_table[fd_idx].fd_inode->write_deny = false;
struct dir_entry new_dir_entry;
memset(&new_dir_entry, 0, sizeof(struct dir_entry));
create_dir_entry(filename, inode_no, FT_REGULAR, &new_dir_entry); // create_dir_entry只是内存操作不出意外,不会返回失败
/* 同步内存数据到硬盘 */
/* a 在目录parent_dir下安装目录项new_dir_entry, 写入硬盘后返回true,否则false */
if (!sync_dir_entry(parent_dir, &new_dir_entry, io_buf))
{
printk("sync dir_entry to disk failed\n");
rollback_step = 3;
goto rollback;
}
memset(io_buf, 0, 1024);
/* b 将父目录i结点的内容同步到硬盘 */
inode_sync(cur_part, parent_dir->inode, io_buf);
memset(io_buf, 0, 1024);
/* c 将新创建文件的i结点内容同步到硬盘 */
inode_sync(cur_part, new_file_inode, io_buf);
/* d 将inode_bitmap位图同步到硬盘 */
bitmap_sync(cur_part, inode_no, INODE_BITMAP);
/* e 将创建的文件i结点添加到open_inodes链表 */
list_push(&cur_part->open_inodes, &new_file_inode->inode_tag);
new_file_inode->i_open_cnts = 1;
sys_free(io_buf);
return pcb_fd_install(fd_idx);
/*创建文件需要创建相关的多个资源,若某步失败则会执行到下面的回滚步骤 */
rollback:
switch (rollback_step)
{
case 3:
/* 失败时,将file_table中的相应位清空 */
memset(&file_table[fd_idx], 0, sizeof(struct file));
case 2:
sys_free(new_file_inode);
case 1:
/* 如果新文件的i结点创建失败,之前位图中分配的inode_no也要恢复 */
bitmap_set(&cur_part->inode_bitmap, inode_no, 0);
break;
}
sys_free(io_buf);
return -1;
}
file_create 用于创建文件,若成功就会返回文件描述符。核心步骤:1,创建新文件的 inode,其中涉及 inode 位图修改,inode 初始化;2,文件结构创建,因为文件结构是描述进程或线程对于文件的操作状态,创建自然也属于这种操作状态。其中涉及全局打开文件结构数组更改;3、创建对应目录项;4、同步新文件的目录项到父目录中去;同步父目录 inode,父节点 inode 其中会涉及 inode->i_size,inode->i_sectors[ ](因为文件的目录项会存在父目录文件中,可能会涉及新的块申请)。同步 inode 也会同步磁盘中的 inode 数组;创建文件的 inode;inode 位图;5、将新文件的 inode 加入打开链表;6、在创建文件进程或线程的 pcb 中安装文件描述符;
sys_open
/* 打开或创建文件成功后,返回文件描述符,否则返回-1 */
int32_t sys_open(const char *pathname, uint8_t flags)
{
/* 对目录要用dir_open,这里只有open文件 */
// 对pathname的最后一个字符判断
if (pathname[strlen(pathname) - 1] == '/')
{
printk("can`t open a directory %s\n", pathname);
return -1;
}
ASSERT(flags <= 7);
int32_t fd = -1; // 默认为找不到
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
/* 记录目录深度.帮助判断中间某个目录不存在的情况 */
uint32_t pathname_depth = path_depth_cnt((char *)pathname);
/* 先检查文件是否存在 */
int inode_no = search_file(pathname, &searched_record);
bool found = inode_no != -1 ? true : false;
if (searched_record.file_type == FT_DIRECTORY)
{
printk("can`t open a direcotry with open(), use opendir() to instead\n");
dir_close(searched_record.parent_dir);
return -1;
}
uint32_t path_searched_depth = path_depth_cnt(searched_record.searched_path);
/* 先判断是否把pathname的各层目录都访问到了,即是否在某个中间目录就失败了 */
if (pathname_depth != path_searched_depth)
{ // 说明并没有访问到全部的路径,某个中间目录是不存在的
printk("cannot access %s: Not a directory, subpath %s is`t exist\n",
pathname, searched_record.searched_path);
dir_close(searched_record.parent_dir);
return -1;
}
/* 若是在最后一个路径上没找到,并且并不是要创建文件,直接返回-1 */
if (!found && !(flags & O_CREAT))
{
printk("in path %s, file %s is`t exist\n",
searched_record.searched_path,
(strrchr(searched_record.searched_path, '/') + 1));
dir_close(searched_record.parent_dir);
return -1;
}
else if (found && flags & O_CREAT)
{ // 若要创建的文件已存在
printk("%s has already exist!\n", pathname);
dir_close(searched_record.parent_dir);
return -1;
}
switch (flags & O_CREAT)
{
case O_CREAT:
printk("creating file\n");
fd = file_create(searched_record.parent_dir, (strrchr(pathname, '/') + 1), flags);
dir_close(searched_record.parent_dir);
// 其余为打开文件
}
/* 此fd是指任务pcb->fd_table数组中的元素下标,
* 并不是指全局file_table中的下标 */
return fd;
}
sys_open 用于根据传入的路径打开或创建文件,并返回文件描述符,由于现在是写个初版,我们只实现创建功能。核心原理就是:先调用 search_file 来查找指定路径是否有这个文件,如果没有,直接调用 file_create 来创建文件。从查找是否存在,到最后创建之间,要排除以下情况:1、找到了,但是找到的是个目录;2、查找过程中某个中间路径就不存在,比如我要创建/home/kanshan/test.c,但是调用 search_file 之后,发现,这个/kanshan 目录都不存在;3、在最后一个路径上没找到,但是没有传入表示创建文件的标志;4、要创建的文件已经存在;
main 修改如下:
#include "print.h"
#include "init.h"
#include "fs.h"
int main(void) {
put_str("I am kernel\n");
init_all();
sys_open("/file1", O_CREAT);
while(1);
return 0;
}
输出了创建文件的信息,去磁盘上看一下:
首先得到根目录的位置
用 xxd 看一下:
一个目录项 24 字节,0x2e 对应. ,所以第一个目录项是./,第二个是../,第三个就是我们创建的文件 file1,最下面的两个 1 分别代表 inode 编号以及文件类型
d 文件的打开与关闭
打开
file_open
/* 打开编号为inode_no的inode对应的文件,若成功则返回文件描述符,否则返回-1 */
int32_t file_open(uint32_t inode_no, uint8_t flag){
int fd_idx = get_free_slot_in_global();
if (fd_idx == -1)
{
printk("exceed max open files\n");
return -1;
}
file_table[fd_idx].fd_inode = inode_open(cur_part, inode_no);
file_table[fd_idx].fd_pos = 0; // 每次打开文件,要将fd_pos还原为0,即让文件内的指针指向开头
file_table[fd_idx].fd_flag = flag;
bool *write_deny = &file_table[fd_idx].fd_inode->write_deny;
if (flag & O_WRONLY || flag & O_RDWR)
{ // 只要是关于写文件,判断是否有其它进程正写此文件
// 若是读文件,不考虑write_deny
/* 以下进入临界区前先关中断 */
enum intr_status old_status = intr_disable();
if (!(*write_deny))
{ // 若当前没有其它进程写该文件,将其占用.
*write_deny = true; // 置为true,避免多个进程同时写此文件
intr_set_status(old_status); // 恢复中断
}
else
{ // 直接失败返回
intr_set_status(old_status);
file_table[fd_idx].fd_inode = NULL;
printk("file can`t be write now, try again later\n");
return -1;
}
} // 若是读文件或创建文件,不用理会write_deny,保持默认
return pcb_fd_install(fd_idx);
}
file_open:用于打开文件,实质就是让文件结构指向一个内存中的 inode。原理:传入需要打开文件的 inode 数组索引,调用 get_free_slot_in_global 在全局打开文件结构数组中找个空位,调用 inode_open 将 inode 调入内存中,然后让该文件结构指向这个 inode,最后调用 pcb_fd_install 安装文件描述符。为了同步性,以写的方式打开的文件不应该是正在写入的文件,需要判断该文件对应 inode 中的正在写入标志。
sys_open
改进
......
......
switch (flags & O_CREAT)
{
case O_CREAT:
printk("creating file\n");
fd = file_create(searched_record.parent_dir, (strrchr(pathname, '/') + 1), flags);
dir_close(searched_record.parent_dir);
// 其余为打开文件
default:
/* 其余情况均为打开已存在文件:
* O_RDONLY,O_WRONLY,O_RDWR */
fd = file_open(inode_no, flags);
}
关闭
file_close
关闭文件
/* 关闭文件 */
int32_t file_close(struct file *file)
{
if (file == NULL)
{
return -1;
}
file->fd_inode->write_deny = false;
inode_close(file->fd_inode);
file->fd_inode = NULL; // 使文件结构可用
return 0;
}
file_close
用传入的文件结构指针关闭文件。步骤:将文件正在写标志关闭,调用 inode_close
移除内存中的 inode,并解除文件结构与 inode 的关系。
fd_local2global sys_close
/* 将文件描述符转化为文件表的下标 */
static uint32_t fd_local2global(uint32_t local_fd)
{
struct task_struct *cur = running_thread();
int32_t global_fd = cur->fd_table[local_fd];
ASSERT(global_fd >= 0 && global_fd < MAX_FILE_OPEN);
return (uint32_t)global_fd;
}
/* 关闭文件描述符fd指向的文件,成功返回0,否则返回-1 */
int32_t sys_close(int32_t fd)
{
int32_t ret = -1; // 返回值默认为-1,即失败
if (fd > 2)
{
uint32_t _fd = fd_local2global(fd);
ret = file_close(&file_table[_fd]);
running_thread()->fd_table[fd] = -1; // 使该文件描述符位可用
}
return ret;
}
fd_local2global
,用于将文件描述符转换为全局打开文件结构数组下标,原理就是去当前正在运行的进程或线程的 pcb 中的 fd_table 数组中找到文件描述符对应的元素,这里面存的就是全局打开文件结构数组下标。
sys_close
用传入的文件描述符关闭文件,原理:调用 fd_close2global
将文件描述符转换为全局打开文件结构数组下标,然后调用 file_close
关闭这个文件结构对应的文件,最后清空 pcb 中的文件描述符位
main.c 修改如下:
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
int main(void) {
put_str("I am kernel\n");
init_all();
uint32_t fd = sys_open("/file1", O_RDONLY);
printf("fd:%d\n", fd);
sys_close(fd);
printf("%d closed now\n", fd);
while(1);
return 0;
}
符合预期:
e 文件写入
file_write
把 buf 中的 count 个字节写入 file
/* 把buf中的count个字节写入file,成功则返回写入的字节数,失败则返回-1 */
int32_t file_write(struct file *file, const void *buf, uint32_t count)
{
if ((file->fd_inode->i_size + count) > (BLOCK_SIZE * 140))
{ // 文件目前最大只支持512*140=71680字节
printk("exceed max file_size 71680 bytes, write file failed\n");
return -1;
}
uint8_t *io_buf = sys_malloc(BLOCK_SIZE);
if (io_buf == NULL)
{
printk("file_write: sys_malloc for io_buf failed\n");
return -1;
}
uint32_t *all_blocks = (uint32_t *)sys_malloc(BLOCK_SIZE + 48); // 用来记录文件所有的块地址
if (all_blocks == NULL)
{
printk("file_write: sys_malloc for all_blocks failed\n");
return -1;
}
const uint8_t *src = buf; // 用src指向buf中待写入的数据
uint32_t bytes_written = 0; // 用来记录已写入数据大小
uint32_t size_left = count; // 用来记录未写入数据大小
int32_t block_lba = -1; // 块地址
uint32_t block_bitmap_idx = 0; // 用来记录block对应于block_bitmap中的索引,做为参数传给bitmap_sync
uint32_t sec_idx; // 用来索引扇区
uint32_t sec_lba; // 扇区地址
uint32_t sec_off_bytes; // 扇区内字节偏移量
uint32_t sec_left_bytes; // 扇区内剩余字节量
uint32_t chunk_size; // 每次写入硬盘的数据块大小
int32_t indirect_block_table; // 用来获取一级间接表地址
uint32_t block_idx; // 块索引
/* 判断文件是否是第一次写,如果是,先为其分配一个块 */
if (file->fd_inode->i_sectors[0] == 0)
{
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("file_write: block_bitmap_alloc failed\n");
return -1;
}
file->fd_inode->i_sectors[0] = block_lba;
/* 每分配一个块就将位图同步到硬盘 */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
ASSERT(block_bitmap_idx != 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
}
/* 写入count个字节前,该文件已经占用的块数 */
uint32_t file_has_used_blocks = file->fd_inode->i_size / BLOCK_SIZE + 1;
// 其实这两行应该用DIV_ROUND_UP进行计算,但是改了之后,后面会涉及大量小细节修改。反正这个能跑,错就错吧
/* 存储count字节后该文件将占用的块数 */
uint32_t file_will_use_blocks = (file->fd_inode->i_size + count) / BLOCK_SIZE + 1;
ASSERT(file_will_use_blocks <= 140);
/* 通过此增量判断是否需要分配扇区,如增量为0,表示原扇区够用 */
uint32_t add_blocks = file_will_use_blocks - file_has_used_blocks;
/* 开始将文件所有块地址收集到all_blocks,(系统中块大小等于扇区大小)
* 后面都统一在all_blocks中获取写入扇区地址 */
if (add_blocks == 0)
{
/* 在同一扇区内写入数据,不涉及到分配新扇区 */
if (file_has_used_blocks <= 12)
{ // 文件数据量将在12块之内
block_idx = file_has_used_blocks - 1; // 指向最后一个已有数据的扇区
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
}
else
{
/* 未写入新数据之前已经占用了间接块,需要将间接块地址读进来 */
ASSERT(file->fd_inode->i_sectors[12] != 0);
indirect_block_table = file->fd_inode->i_sectors[12];
ide_read(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1);
}
}
else
{
/* 若有增量,便涉及到分配新扇区及是否分配一级间接块表,下面要分三种情况处理 */
/* 第一种情况:12个直接块够用*/
if (file_will_use_blocks <= 12)
{
/* 先将有剩余空间的可继续用的扇区地址写入all_blocks */
block_idx = file_has_used_blocks - 1;
ASSERT(file->fd_inode->i_sectors[block_idx] != 0);
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
/* 再将未来要用的扇区分配好后写入all_blocks */
block_idx = file_has_used_blocks; // 指向第一个要分配的新扇区
while (block_idx < file_will_use_blocks)
{
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("file_write: block_bitmap_alloc for situation 1 failed\n");
return -1;
}
/* 写文件时,不应该存在块未使用但已经分配扇区的情况,当文件删除时,就会把块地址清0 */
ASSERT(file->fd_inode->i_sectors[block_idx] == 0); // 确保尚未分配扇区地址
file->fd_inode->i_sectors[block_idx] = all_blocks[block_idx] = block_lba;
/* 每分配一个块就将位图同步到硬盘 */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
block_idx++; // 下一个分配的新扇区
}
}
else if (file_has_used_blocks <= 12 && file_will_use_blocks > 12)
{
/* 第二种情况: 旧数据在12个直接块内,新数据将使用间接块*/
/* 先将有剩余空间的可继续用的扇区地址收集到all_blocks */
block_idx = file_has_used_blocks - 1; // 指向旧数据所在的最后一个扇区
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
/* 创建一级间接块表 */
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("file_write: block_bitmap_alloc for situation 2 failed\n");
return -1;
}
ASSERT(file->fd_inode->i_sectors[12] == 0); // 确保一级间接块表未分配
/* 分配一级间接块索引表 */
indirect_block_table = file->fd_inode->i_sectors[12] = block_lba;
block_idx = file_has_used_blocks; // 第一个未使用的块,即本文件最后一个已经使用的直接块的下一块
while (block_idx < file_will_use_blocks)
{
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("file_write: block_bitmap_alloc for situation 2 failed\n");
return -1;
}
if (block_idx < 12)
{ // 新创建的0~11块直接存入all_blocks数组
ASSERT(file->fd_inode->i_sectors[block_idx] == 0); // 确保尚未分配扇区地址
file->fd_inode->i_sectors[block_idx] = all_blocks[block_idx] = block_lba;
}
else // 间接块只写入到all_block数组中,待全部分配完成后一次性同步到硬盘
all_blocks[block_idx] = block_lba;
/* 每分配一个块就将位图同步到硬盘 */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
block_idx++; // 下一个新扇区
}
ide_write(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1);
}
else if (file_has_used_blocks > 12)
{
/* 第三种情况:新数据占据间接块*/
ASSERT(file->fd_inode->i_sectors[12] != 0); // 已经具备了一级间接块表
indirect_block_table = file->fd_inode->i_sectors[12]; // 获取一级间接表地址
/* 已使用的间接块也将被读入all_blocks,无须单独收录 */
ide_read(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1); // 获取所有间接块地址
block_idx = file_has_used_blocks; // 第一个未使用的间接块,即已经使用的间接块的下一块
while (block_idx < file_will_use_blocks)
{
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("file_write: block_bitmap_alloc for situation 3 failed\n");
return -1;
}
all_blocks[block_idx++] = block_lba;
/* 每分配一个块就将位图同步到硬盘 */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
}
ide_write(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1); // 同步一级间接块表到硬盘
}
}
bool first_write_block = true; // 含有剩余空间的扇区标识
/* 块地址已经收集到all_blocks中,下面开始写数据 */
file->fd_pos = file->fd_inode->i_size - 1; // 置fd_pos为文件大小-1,下面在写数据时随时更新
while (bytes_written < count)
{ // 直到写完所有数据
memset(io_buf, 0, BLOCK_SIZE);
sec_idx = file->fd_inode->i_size / BLOCK_SIZE;
sec_lba = all_blocks[sec_idx];
sec_off_bytes = file->fd_inode->i_size % BLOCK_SIZE;
sec_left_bytes = BLOCK_SIZE - sec_off_bytes;
/* 判断此次写入硬盘的数据大小 */
chunk_size = size_left < sec_left_bytes ? size_left : sec_left_bytes;
if (first_write_block)
{
ide_read(cur_part->my_disk, sec_lba, io_buf, 1);
first_write_block = false;
}
memcpy(io_buf + sec_off_bytes, src, chunk_size);
ide_write(cur_part->my_disk, sec_lba, io_buf, 1);
printk("file write at lba 0x%x\n", sec_lba); // 调试,完成后去掉
src += chunk_size; // 将指针推移到下个新数据
file->fd_inode->i_size += chunk_size; // 更新文件大小
file->fd_pos += chunk_size;
bytes_written += chunk_size;
size_left -= chunk_size;
}
inode_sync(cur_part, file->fd_inode, io_buf);
sys_free(all_blocks);
sys_free(io_buf);
return bytes_written;
}
file_write,用于把 buf 中的 count 个字节写入文件。核心原理:传进函数的文件结构 struct file 指针中有个指向操作文件 inode 的指针,通过这个 inode 中的 i_size 与 i_sectors[ ],我们可以顺利知道文件大小与存储位置信息。先将文件已有数据的最后一块数据读出来并与将要写入的数据在缓冲区中共同拼凑成一个完整的块,然后写入磁盘。剩下的数据以块为单位继续写入磁盘即可。麻烦的是对块地址的处理,分成只有直接块,直接块和间接块混合,只有间接块三种情况。
sys_write
将 buf 中连续 count 个字节写入文件描述符 fd
/* 将buf中连续count个字节写入文件描述符fd,成功则返回写入的字节数,失败返回-1 */
int32_t sys_write(int32_t fd, const void *buf, uint32_t count)
{
if (fd < 0)
{
printk("sys_write: fd error\n");
return -1;
}
// 标准输出
if (fd == stdout_no)
{
char tmp_buf[1024] = {0};
memcpy(tmp_buf, buf, count);
console_put_str(tmp_buf);
return count;
}
uint32_t _fd = fd_local2global(fd);
struct file *wr_file = &file_table[_fd];
if (wr_file->fd_flag & O_WRONLY || wr_file->fd_flag & O_RDWR)
{
uint32_t bytes_written = file_write(wr_file, buf, count);
return bytes_written;
}
else
{
console_put_str("sys_write: not allowed to write file without flag O_RDWR or O_WRONLY\n");
return -1;
}
}
sys_write
用于将 buf 中连续 count 个字节写入文件描述符 fd。如果传入的 fd 表示标准输出,直接调用 console_put_str 打印即可。如果不是,直接调用 file_write
即可
这样我们之前的 sys_write
以及用户态的 write
定义都要改,不再展示
main.c 修改如下:
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
int main(void) {
put_str("I am kernel\n");
init_all();
uint32_t fd = sys_open("/file1", O_RDWR);
printf("fd:%d\n", fd);
sys_write(fd, "hello,world\n", 12);
sys_close(fd);
printf("%d closed now\n", fd);
while(1);
return 0;
}
运行结果如下:
可以看到写的地址为 0xA6C,用 xxd 看一下:xxd -l 512 -s 1366016 hd80M.img
再运行一遍试试,在后面追加了 hello world,符合预期
f 文件读取
file_read
/* 从文件file中读取count个字节写入buf, 返回读出的字节数,若到文件尾则返回-1 */
int32_t file_read(struct file *file, void *buf, uint32_t count)
{
uint8_t *buf_dst = (uint8_t *)buf;
uint32_t size = count, size_left = size;
/* 若要读取的字节数超过了文件可读的剩余量, 就用剩余量做为待读取的字节数 */
if ((file->fd_pos + count) > file->fd_inode->i_size)
{
size = file->fd_inode->i_size - file->fd_pos;
size_left = size;
if (size == 0)
{ // 若到文件尾则返回-1
return -1;
}
}
uint8_t *io_buf = sys_malloc(BLOCK_SIZE);
if (io_buf == NULL)
{
printk("file_read: sys_malloc for io_buf failed\n");
}
uint32_t *all_blocks = (uint32_t *)sys_malloc(BLOCK_SIZE + 48); // 用来记录文件所有的块地址
if (all_blocks == NULL)
{
printk("file_read: sys_malloc for all_blocks failed\n");
return -1;
}
int32_t block_read_start_idx = file->fd_pos / BLOCK_SIZE; // 数据所在块的起始地址
uint32_t block_read_end_idx = (file->fd_pos + size) / BLOCK_SIZE; // 数据所在块的终止地址
uint32_t read_blocks = block_read_start_idx - block_read_end_idx; // 如增量为0,表示数据在同一扇区
ASSERT(block_read_start_idx < 140 && block_read_end_idx < 140);
int32_t indirect_block_table; // 用来获取一级间接表地址
uint32_t block_idx; // 获取待读的块地址
/* 以下开始构建all_blocks块地址数组,专门存储用到的块地址(本程序中块大小同扇区大小) */
if (read_blocks == 0)
{ // 在同一扇区内读数据,不涉及到跨扇区读取
ASSERT(block_read_end_idx == block_read_start_idx);
if (block_read_end_idx < 12)
{ // 待读的数据在12个直接块之内
block_idx = block_read_end_idx;
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
}
else
{ // 若用到了一级间接块表,需要将表中间接块读进来
indirect_block_table = file->fd_inode->i_sectors[12];
ide_read(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1);
}
}
else
{
// 若要读多个块
/* 第一种情况: 起始块和终止块属于直接块*/
if (block_read_end_idx < 12)
{ // 数据结束所在的块属于直接块
block_idx = block_read_start_idx;
while (block_idx <= block_read_end_idx)
{
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
block_idx++;
}
}
else if (block_read_start_idx < 12 && block_read_end_idx >= 12)
{
/* 第二种情况: 待读入的数据跨越直接块和间接块两类*/
/* 先将直接块地址写入all_blocks */
block_idx = block_read_start_idx;
while (block_idx < 12)
{
all_blocks[block_idx] = file->fd_inode->i_sectors[block_idx];
block_idx++;
}
ASSERT(file->fd_inode->i_sectors[12] != 0); // 确保已经分配了一级间接块表
/* 再将间接块地址写入all_blocks */
indirect_block_table = file->fd_inode->i_sectors[12];
ide_read(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1); // 将一级间接块表读进来写入到第13个块的位置之后
}
else
{
/* 第三种情况: 数据在间接块中*/
ASSERT(file->fd_inode->i_sectors[12] != 0); // 确保已经分配了一级间接块表
indirect_block_table = file->fd_inode->i_sectors[12]; // 获取一级间接表地址
ide_read(cur_part->my_disk, indirect_block_table, all_blocks + 12, 1); // 将一级间接块表读进来写入到第13个块的位置之后
}
}
/* 用到的块地址已经收集到all_blocks中,下面开始读数据 */
uint32_t sec_idx, sec_lba, sec_off_bytes, sec_left_bytes, chunk_size;
uint32_t bytes_read = 0;
while (bytes_read < size)
{ // 直到读完为止
sec_idx = file->fd_pos / BLOCK_SIZE;
sec_lba = all_blocks[sec_idx];
sec_off_bytes = file->fd_pos % BLOCK_SIZE;
sec_left_bytes = BLOCK_SIZE - sec_off_bytes;
chunk_size = size_left < sec_left_bytes ? size_left : sec_left_bytes; // 待读入的数据大小
memset(io_buf, 0, BLOCK_SIZE);
ide_read(cur_part->my_disk, sec_lba, io_buf, 1);
memcpy(buf_dst, io_buf + sec_off_bytes, chunk_size);
buf_dst += chunk_size;
file->fd_pos += chunk_size;
bytes_read += chunk_size;
size_left -= chunk_size;
}
sys_free(all_blocks);
sys_free(io_buf);
return bytes_read;
}
file_read 从文件 file 中读取 count 个字节到 buf。核心原理:文件结构 struct file 内有个 fd_pos 表示当前操作的文件内容位置,其实就是要读取的内容在文件中的起始位置,比如 1 个 1KB 大小的文本文件,fd_pos = 500,那么就是表示读取当前文件中偏移 500 字节的这个字符开始的内容。通过 struct file 中的 fd_pos 与传入函数的 count,我们可以确定要读取的内容相对于整个文件的字节偏移量。然后 struct file 中有个指向操作文件 inode 的指 针,通过这个 inode 中的 i_size 与 i_sectors[ ],我们可以顺利知道文件存储位置信息。所以,自然就能知道要读取内容在磁盘中的位置。由于我们操作单位是块,所以对于起始位置,我们要抛弃这个块前面的无用数据,对于结束位置,我们要抛弃这个块后面的无用数据。
sys_read
/* 从文件描述符fd指向的文件中读取count个字节到buf,若成功则返回读出的字节数,到文件尾则返回-1 */
int32_t sys_read(int32_t fd, void *buf, uint32_t count)
{
if (fd < 0)
{
printk("sys_read: fd error\n");
return -1;
}
ASSERT(buf != NULL);
uint32_t _fd = fd_local2global(fd);
return file_read(&file_table[_fd], buf, count);
}
sys_read
根据传入的文件描述符,调用 fd_local2global
将文件描述符转换成指定的全局打开文件结构数组索引,然后调用 file_read
读出 count 字节放入 buf
中
main.c 如下:
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
int main(void) {
put_str("I am kernel\n");
init_all();
uint32_t fd = sys_open("/file1", O_RDWR);
printf("open /file1, fd:%d\n", fd);
char buf[64] = {0};
int read_bytes = sys_read(fd, buf, 18);
printf("1_ read %d bytes:\n%s\n", read_bytes, buf);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 6);
printf("2_ read %d bytes:\n%s", read_bytes, buf);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 6);
printf("3_ read %d bytes:\n%s", read_bytes, buf);
printf("________ close file1 and reopen ________\n");
sys_close(fd);
fd = sys_open("/file1", O_RDWR);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 24);
printf("4_ read %d bytes:\n%s", read_bytes, buf);
sys_close(fd);
while(1);
return 0;
}
运行结果如下,符合预期:
g 文件指针重定位
/* 重置用于文件读写操作的偏移指针,成功时返回新的偏移量,出错时返回-1 */
int32_t sys_lseek(int32_t fd, int32_t offset, uint8_t whence)
{
if (fd < 0)
{
printk("sys_lseek: fd error\n");
return -1;
}
ASSERT(whence > 0 && whence < 4);
uint32_t _fd = fd_local2global(fd);
struct file *pf = &file_table[_fd];
int32_t new_pos = 0; // 新的偏移量必须位于文件大小之内
int32_t file_size = (int32_t)pf->fd_inode->i_size;
switch (whence)
{
/* SEEK_SET 新的读写位置是相对于文件开头再增加offset个位移量 */
case SEEK_SET:
new_pos = offset;
break;
/* SEEK_CUR 新的读写位置是相对于当前的位置增加offset个位移量 */
case SEEK_CUR: // offse可正可负
new_pos = (int32_t)pf->fd_pos + offset;
break;
/* SEEK_END 新的读写位置是相对于文件尺寸再增加offset个位移量 */
case SEEK_END: // 此情况下,offset应该为负值
new_pos = file_size + offset;
}
if (new_pos < 0 || new_pos > (file_size - 1))
{
return -1;
}
pf->fd_pos = new_pos;
return pf->fd_pos;
}
sys_lseek 用于根据传入的参照物与偏移,重置传入的文件描述符对应的全局打开文件结构中的 fd_pos。核心原理:根据传入的文件描述符,调用 fd_local2global 将文件描述符转换成指定的全局打开文件结构数组索引,然后 switch case 对传入的参照物情况进行选择,以对 fd_pos 做不同处理。参照物为 SEET_SET,那么新的 fd_pos 就等于传入的偏移;参照物为 SEET_CUR,那么新的 fd_pos = 原来 fd_pos + 传入偏移;参照物为 SEET_END,那么新的 fd_pos 为原来文件大小 + 偏移(此时偏移量不出意外的话是负数)
测试代码 main.c
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
#include "string.h"
int main(void)
{
put_str("I am kernel\n");
init_all();
uint32_t fd = sys_open("/file1", O_RDWR);
printf("open /file1, fd:%d\n", fd);
char buf[64] = {0};
int read_bytes = sys_read(fd, buf, 18);
printf("1_ read %d bytes:\n%s\n", read_bytes, buf);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 6);
printf("2_ read %d bytes:\n%s", read_bytes, buf);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 6);
printf("3_ read %d bytes:\n%s", read_bytes, buf);
printf("________ SEEK_SET 0 ________\n");
sys_lseek(fd, 0, SEEK_SET);
memset(buf, 0, 64);
read_bytes = sys_read(fd, buf, 24);
printf("4_ read %d bytes:\n%s", read_bytes, buf);
sys_close(fd);
while (1)
;
return 0;
}
符合预期
h 文件删除
回收 inode
删除文件最重要的就是回收文件对应的 inode。与 inode 相关的资源有
- inode 位图
- inode_table
- inode 中 i_sectors[0~11]中的直接块和一级间接索引块表 i_sectors[12]中的间接块
- 一级间接索引块表本身的扇区地址
inode_delete inode_release
/* 将硬盘分区part上的inode清空 */
void inode_delete(struct partition *part, uint32_t inode_no, void *io_buf){
ASSERT(inode_no < 4096);
struct inode_position inode_pos;
inode_locate(part, inode_no, &inode_pos); // inode位置信息会存入inode_pos
ASSERT(inode_pos.sec_lba <= (part->start_lba + part->sec_cnt));
char *inode_buf = (char *)io_buf;
if (inode_pos.two_sec)
{ // inode跨扇区,读入2个扇区
/* 将原硬盘上的内容先读出来 */
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 2);
/* 将inode_buf清0 */
memset((inode_buf + inode_pos.off_size), 0, sizeof(struct inode));
/* 用清0的内存数据覆盖磁盘 */
ide_write(part->my_disk, inode_pos.sec_lba, inode_buf, 2);
}
else
{ // 未跨扇区,只读入1个扇区就好
/* 将原硬盘上的内容先读出来 */
ide_read(part->my_disk, inode_pos.sec_lba, inode_buf, 1);
/* 将inode_buf清0 */
memset((inode_buf + inode_pos.off_size), 0, sizeof(struct inode));
/* 用清0的内存数据覆盖磁盘 */
ide_write(part->my_disk, inode_pos.sec_lba, inode_buf, 1);
}
}
/* 回收inode的数据块和inode本身 */
void inode_release(struct partition *part, uint32_t inode_no){
/* 1 回收inode占用的所有块 */
uint8_t block_idx = 0, block_cnt = 12;
uint32_t block_bitmap_idx;
uint32_t all_blocks[140] = {0}; // 12个直接块+128个间接块
/* a 先将前12个直接块存入all_blocks */
while (block_idx < 12)
{
all_blocks[block_idx] = inode_to_del->i_sectors[block_idx];
block_idx++;
}
/* b 如果一级间接块表存在,将其128个间接块读到all_blocks[12~], 并释放一级间接块表所占的扇区 */
if (inode_to_del->i_sectors[12] != 0)
{
ide_read(part->my_disk, inode_to_del->i_sectors[12], all_blocks + 12, 1);
block_cnt = 140;
/* 回收一级间接块表占用的扇区 */
block_bitmap_idx = inode_to_del->i_sectors[12] - part->sb->data_start_lba;
ASSERT(block_bitmap_idx > 0);
bitmap_set(&part->block_bitmap, block_bitmap_idx, 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
}
/* c inode所有的块地址已经收集到all_blocks中,下面逐个回收 */
block_idx = 0;
while (block_idx < block_cnt)
{
if (all_blocks[block_idx] != 0)
{
block_bitmap_idx = 0;
block_bitmap_idx = all_blocks[block_idx] - part->sb->data_start_lba;
ASSERT(block_bitmap_idx > 0);
bitmap_set(&part->block_bitmap, block_bitmap_idx, 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
}
block_idx++;
}
/*2 回收该inode所占用的inode */
bitmap_set(&part->inode_bitmap, inode_no, 0);
bitmap_sync(cur_part, inode_no, INODE_BITMAP);
/****** 以下inode_delete是调试用的 ******
* 此函数会在inode_table中将此inode清0,
* 但实际上是不需要的,inode分配是由inode位图控制的,
* 硬盘上的数据不需要清0,可以直接覆盖*/
void *io_buf = sys_malloc(1024);
inode_delete(part, inode_no, io_buf);
sys_free(io_buf);
/***********************************************/
inode_close(inode_to_del);
}
inode_delete,传入要删除的 inode 在 inode 数组中的索引,然后删除磁盘中的这个 inode。这个函数可有可无,因为 inode 分配是依靠 inode 位图来完成的,我们回收一个 inode,只需要回收一个 inode 位图中的位即可。因为下次分配这个 inode 位之后,新的 inode 数据会覆盖旧有 inode 数据,这样还能避免不必要的磁盘读写。函数原理:调用 inode_locate 可以将 inode 数组索引转换为这个 inode 在磁盘中的起始扇区和字节偏移。我们将这个 inode 所在整体扇区读出内存缓冲区中,然后将这个内存缓冲区中的 inode 清除,再将内存缓冲区中的数据写回磁盘中即可。
inode_release 删除文件,包含:删除磁盘中的 inode,并回收文件占用的块。这个过程包含:1、回收文件所占用的所有块,通过 inode 中的 i_sectors[ ]即可知道占用了哪些块,然后去清除对应的块位图中的位即可,也就是并没有真正删除文件数据;2、回收 inode,首先清除该 inode 在 inode 位图中对应的位,然后调用
回收目录
文件名是以目录项的形式存在的,删除文件必须在目录中将其目录项擦除。下面看一下删除目录项相关的工作。
- 在文件所在的目录中擦除该文件的目录项,使其为 0。
- 根目录是必须存在的,它是文件读写的根基,不应该被清空,它至少要保留 1 个块。如果目录项独占 1 个块,并且该块不是根目录最后一个块的话,将其回收。
- 目录 inode 的 i_size 是目录项大小的总和,因此还要将 i_size 减去一个目录项的单位大小。
- 目录 inode 改变后,要同步到硬盘。
delete_dir_entry
/* 把分区part目录pdir中编号为inode_no的目录项删除 */
bool delete_dir_entry(struct partition *part, struct dir *pdir, uint32_t inode_no, void *io_buf)
{
struct inode *dir_inode = pdir->inode;
uint32_t block_idx = 0, all_blocks[140] = {0};
/* 收集目录全部块地址 */
while (block_idx < 12)
{
all_blocks[block_idx] = dir_inode->i_sectors[block_idx];
block_idx++;
}
if (dir_inode->i_sectors[12])
{
ide_read(part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
}
/* 目录项在存储时保证不会跨扇区 */
uint32_t dir_entry_size = part->sb->dir_entry_size;
uint32_t dir_entrys_per_sec = (SECTOR_SIZE / dir_entry_size); // 每扇区最大的目录项数目
struct dir_entry *dir_e = (struct dir_entry *)io_buf;
struct dir_entry *dir_entry_found = NULL;
uint8_t dir_entry_idx, dir_entry_cnt;
bool is_dir_first_block = false; // 目录的第1个块
/* 遍历所有块,寻找目录项 */
block_idx = 0;
while (block_idx < 140)
{
is_dir_first_block = false;
if (all_blocks[block_idx] == 0)
{
block_idx++;
continue;
}
dir_entry_idx = dir_entry_cnt = 0;
memset(io_buf, 0, SECTOR_SIZE);
/* 读取扇区,获得目录项 */
ide_read(part->my_disk, all_blocks[block_idx], io_buf, 1);
/* 遍历所有的目录项,统计该扇区的目录项数量及是否有待删除的目录项 */
while (dir_entry_idx < dir_entrys_per_sec)
{
if ((dir_e + dir_entry_idx)->f_type != FT_UNKNOWN)
{
if (!strcmp((dir_e + dir_entry_idx)->filename, "."))
is_dir_first_block = true;
else if (strcmp((dir_e + dir_entry_idx)->filename, ".") && strcmp((dir_e + dir_entry_idx)->filename, ".."))
{
dir_entry_cnt++; // 统计此扇区内的目录项个数,用来判断删除目录项后是否回收该扇区
if ((dir_e + dir_entry_idx)->i_no == inode_no)
{ // 如果找到此i结点,就将其记录在dir_entry_found
ASSERT(dir_entry_found == NULL); // 确保目录中只有一个编号为inode_no的inode,找到一次后dir_entry_found就不再是NULL
dir_entry_found = dir_e + dir_entry_idx;
/* 找到后也继续遍历,统计总共的目录项数 */
}
}
}
dir_entry_idx++;
}
/* 若此扇区未找到该目录项,继续在下个扇区中找 */
if (dir_entry_found == NULL)
{
block_idx++;
continue;
}
/* 在此扇区中找到目录项后,清除该目录项并判断是否回收扇区,随后退出循环直接返回 */
ASSERT(dir_entry_cnt >= 1);
/* 除目录第1个扇区外,若该扇区上只有该目录项自己,则将整个扇区回收 */
if (dir_entry_cnt == 1 && !is_dir_first_block)
{
/* a 在块位图中回收该块 */
uint32_t block_bitmap_idx = all_blocks[block_idx] - part->sb->data_start_lba;
bitmap_set(&part->block_bitmap, block_bitmap_idx, 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
/* b 将块地址从数组i_sectors或索引表中去掉 */
if (block_idx < 12)
dir_inode->i_sectors[block_idx] = 0;
else
{
// 在一级间接索引表中擦除该间接块地址
/*先判断一级间接索引表中间接块的数量,如果仅有这1个间接块,连同间接索引表所在的块一同回收 */
uint32_t indirect_blocks = 0;
uint32_t indirect_block_idx = 12;
while (indirect_block_idx < 140)
{
if (all_blocks[indirect_block_idx] != 0)
indirect_blocks++;
}
ASSERT(indirect_blocks >= 1); // 包括当前间接块
if (indirect_blocks > 1)
{ // 间接索引表中还包括其它间接块,仅在索引表中擦除当前这个间接块地址
all_blocks[block_idx] = 0;
ide_write(part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
}
else
{ // 间接索引表中就当前这1个间接块,直接把间接索引表所在的块回收,然后擦除间接索引表块地址
/* 回收间接索引表所在的块 */
block_bitmap_idx = dir_inode->i_sectors[12] - part->sb->data_start_lba;
bitmap_set(&part->block_bitmap, block_bitmap_idx, 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
/* 将间接索引表地址清0 */
dir_inode->i_sectors[12] = 0;
}
}
}
else
{ // 仅将该目录项清空
memset(dir_entry_found, 0, dir_entry_size);
ide_write(part->my_disk, all_blocks[block_idx], io_buf, 1);
}
/* 更新i结点信息并同步到硬盘 */
ASSERT(dir_inode->i_size >= dir_entry_size);
dir_inode->i_size -= dir_entry_size;
memset(io_buf, 0, SECTOR_SIZE * 2);
inode_sync(part, dir_inode, io_buf);
return true;
}
/* 所有块中未找到则返回false,若出现这种情况应该是serarch_file出错了 */
return false;
}
delete_dir_entry 删除指定文件对应在磁盘中的目录项。核心原理:传入的要删除文件的父目录结构体 struct dir 指针内有 inode 成员,这个 inode 内有 i_sectors[ ]记录着这个目录文件的存储位置,我们自然可以以块为单位从磁盘中把父目录文件读取到缓冲区中,然后遍历找到要删除的目录项,删除缓冲区内对应的目录项,然后写回缓冲区数据。
在这个过程中:如果发现该目录项所在块(非.所在块)仅有要删除的这一个目录项,那么就采取回收这个块(删除块位图中的位,然后同步块位图)的方式清除目录项。回收这个块时,还要判断这个块是不是一级间接块当中的唯一一个的那个块,如果是,则还需要回收这个一级间接块。
sys_unlink
/* 删除文件(非目录),成功返回0,失败返回-1 */
int32_t sys_unlink(const char *pathname)
{
ASSERT(strlen(pathname) < MAX_PATH_LEN);
/* 先检查待删除的文件是否存在 */
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
int inode_no = search_file(pathname, &searched_record);
ASSERT(inode_no != 0);
if (inode_no == -1)
{
printk("file %s not found!\n", pathname);
dir_close(searched_record.parent_dir);
return -1;
}
if (searched_record.file_type == FT_DIRECTORY)
{
printk("can`t delete a direcotry with unlink(), use rmdir() to instead\n");
dir_close(searched_record.parent_dir);
return -1;
}
/* 检查是否在已打开文件列表(文件表)中 */
uint32_t file_idx = 0;
while (file_idx < MAX_FILE_OPEN)
{
if (file_table[file_idx].fd_inode != NULL && (uint32_t)inode_no == file_table[file_idx].fd_inode->i_no)
{
break;
}
file_idx++;
}
if (file_idx < MAX_FILE_OPEN)
{
dir_close(searched_record.parent_dir);
printk("file %s is in use, not allow to delete!\n", pathname);
return -1;
}
ASSERT(file_idx == MAX_FILE_OPEN);
/* 为delete_dir_entry申请缓冲区 */
void *io_buf = sys_malloc(SECTOR_SIZE + SECTOR_SIZE);
if (io_buf == NULL)
{
dir_close(searched_record.parent_dir);
printk("sys_unlink: malloc for io_buf failed\n");
return -1;
}
struct dir *parent_dir = searched_record.parent_dir;
delete_dir_entry(cur_part, parent_dir, inode_no, io_buf);
inode_release(cur_part, inode_no);
sys_free(io_buf);
dir_close(searched_record.parent_dir);
return 0; // 成功删除文件
}
sys_unlink 用于根据传入路径,删除非目录文件。原理:首先调用 search_file 搜索路径以返回文件的 inode,判断该 inode 是否对应某个打开全局文件结构,如果是,则说明此文件正在被使用,那么就不应该被删除。如果不是,调用 delete_dir_entry 删除这个文件在磁盘中的目录项,调用 inode_release 删除 inode 对应的文件,这就完成了删除。
测试代码 main.c:
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
#include "string.h"
int main(void) {
put_str("I am kernel\n");
init_all();
printf("/file1 delete %s!\n", sys_unlink("/file1") == 0 ? "done" : "fail");
while(1);
return 0;
}
删除文件之前
之后
i 创建目录
创建目录文件涉及的工作,其实就是涉及到了 inode 与目录项:
-
确认待创建的新目录在文件系统上不存在。
-
为新目录创建 inode。
-
为新目录分配 1 个块存储该目录文件中的目录项。
-
在新目录中创建两个目录项“…”和“.”,这是每个目录都必须存在的两个目录项。
-
在新目录的父目录中添加新目录的目录项。
-
将以上资源的变更同步到磁盘。
sys_mkdir
/* 创建目录pathname,成功返回0,失败返回-1 */
int32_t sys_mkdir(const char *pathname)
{
uint8_t rollback_step = 0; // 用于操作失败时回滚各资源状态
void *io_buf = sys_malloc(SECTOR_SIZE * 2);
if (io_buf == NULL)
{
printk("sys_mkdir: sys_malloc for io_buf failed\n");
return -1;
}
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
int inode_no = -1;
inode_no = search_file(pathname, &searched_record);
if (inode_no != -1)
{ // 如果找到了同名目录或文件,失败返回
printk("sys_mkdir: file or directory %s exist!\n", pathname);
rollback_step = 1;
goto rollback;
}
else
{ // 若未找到,也要判断是在最终目录没找到还是某个中间目录不存在
uint32_t pathname_depth = path_depth_cnt((char *)pathname);
uint32_t path_searched_depth = path_depth_cnt(searched_record.searched_path);
/* 先判断是否把pathname的各层目录都访问到了,即是否在某个中间目录就失败了 */
if (pathname_depth != path_searched_depth)
{ // 说明并没有访问到全部的路径,某个中间目录是不存在的
printk("sys_mkdir: can`t access %s, subpath %s is`t exist\n", pathname, searched_record.searched_path);
rollback_step = 1;
goto rollback;
}
}
struct dir *parent_dir = searched_record.parent_dir;
/* 目录名称后可能会有字符'/',所以最好直接用searched_record.searched_path,无'/' */
char *dirname = strrchr(searched_record.searched_path, '/') + 1;
inode_no = inode_bitmap_alloc(cur_part);
if (inode_no == -1)
{
printk("sys_mkdir: allocate inode failed\n");
rollback_step = 1;
goto rollback;
}
struct inode new_dir_inode;
inode_init(inode_no, &new_dir_inode); // 初始化i结点
uint32_t block_bitmap_idx = 0; // 用来记录block对应于block_bitmap中的索引
int32_t block_lba = -1;
/* 为目录分配一个块,用来写入目录.和.. */
block_lba = block_bitmap_alloc(cur_part);
if (block_lba == -1)
{
printk("sys_mkdir: block_bitmap_alloc for create directory failed\n");
rollback_step = 2;
goto rollback;
}
new_dir_inode.i_sectors[0] = block_lba;
/* 每分配一个块就将位图同步到硬盘 */
block_bitmap_idx = block_lba - cur_part->sb->data_start_lba;
ASSERT(block_bitmap_idx != 0);
bitmap_sync(cur_part, block_bitmap_idx, BLOCK_BITMAP);
/* 将当前目录的目录项'.'和'..'写入目录 */
memset(io_buf, 0, SECTOR_SIZE * 2); // 清空io_buf
struct dir_entry *p_de = (struct dir_entry *)io_buf;
/* 初始化当前目录"." */
memcpy(p_de->filename, ".", 1);
p_de->i_no = inode_no;
p_de->f_type = FT_DIRECTORY;
p_de++;
/* 初始化当前目录".." */
memcpy(p_de->filename, "..", 2);
p_de->i_no = parent_dir->inode->i_no;
p_de->f_type = FT_DIRECTORY;
ide_write(cur_part->my_disk, new_dir_inode.i_sectors[0], io_buf, 1);
new_dir_inode.i_size = 2 * cur_part->sb->dir_entry_size;
/* 在父目录中添加自己的目录项 */
struct dir_entry new_dir_entry;
memset(&new_dir_entry, 0, sizeof(struct dir_entry));
create_dir_entry(dirname, inode_no, FT_DIRECTORY, &new_dir_entry);
memset(io_buf, 0, SECTOR_SIZE * 2); // 清空io_buf
if (!sync_dir_entry(parent_dir, &new_dir_entry, io_buf))
{ // sync_dir_entry中将block_bitmap通过bitmap_sync同步到硬盘
printk("sys_mkdir: sync_dir_entry to disk failed!\n");
rollback_step = 2;
goto rollback;
}
/* 父目录的inode同步到硬盘 */
memset(io_buf, 0, SECTOR_SIZE * 2);
inode_sync(cur_part, parent_dir->inode, io_buf);
/* 将新创建目录的inode同步到硬盘 */
memset(io_buf, 0, SECTOR_SIZE * 2);
inode_sync(cur_part, &new_dir_inode, io_buf);
/* 将inode位图同步到硬盘 */
bitmap_sync(cur_part, inode_no, INODE_BITMAP);
sys_free(io_buf);
/* 关闭所创建目录的父目录 */
dir_close(searched_record.parent_dir);
return 0;
/*创建文件或目录需要创建相关的多个资源,若某步失败则会执行到下面的回滚步骤 */
rollback: // 因为某步骤操作失败而回滚
switch (rollback_step)
{
case 2:
bitmap_set(&cur_part->inode_bitmap, inode_no, 0); // 如果新文件的inode创建失败,之前位图中分配的inode_no也要恢复
case 1:
/* 关闭所创建目录的父目录 */
dir_close(searched_record.parent_dir);
break;
}
sys_free(io_buf);
return -1;
}
sys_mkdir 用于根据传入的路径创建目录文件。核心原理:1、调用 search_file 来确认待创建的新目录文件在文件系统上不存在;2、调用 inode_bitmap_alloc 来为新目录分配 inode 索引,并调用 inode_init 来初始化这个 inode;3、调用 block_bitmap_alloc 来分配一个块用于承载该目录的目录文件。同时设定 inode 的 i_sectors[0],并调用 bitmap_sync 同步块位图;4、清零缓冲区,然后写入.与…两个目录项,之后将缓冲区内容写入到目录文件中,这就完成了.与…两个目录项的创建;5、创建并设定自己的目录项,调用 sync_dir_entry 来将目录项同步到父目录中;6、inode_sync 同步父目录的 inode 与自己的 inode,bitmap_sync 同步 inode 位图
可以根据书 P670 的内容来进行磁盘追踪
j 打开/关闭目录
sys_opendir
sys_closedir
/* 目录打开成功后返回目录指针,失败返回NULL */
struct dir *sys_opendir(const char *name){
ASSERT(strlen(name) < MAX_PATH_LEN);
/* 如果是根目录'/',直接返回&root_dir */
if (name[0] == '/' && (name[1] == 0 || name[0] == '.'))
{
return &root_dir;
}
/* 先检查待打开的目录是否存在 */
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
int inode_no = search_file(name, &searched_record);
struct dir *ret = NULL;
if (inode_no == -1)
{ // 如果找不到目录,提示不存在的路径
printk("In %s, sub path %s not exist\n", name, searched_record.searched_path);
}
else
{
if (searched_record.file_type == FT_REGULAR)
{
printk("%s is regular file!\n", name);
}
else if (searched_record.file_type == FT_DIRECTORY)
{
ret = dir_open(cur_part, inode_no);
}
}
dir_close(searched_record.parent_dir);
return ret;
}
/* 成功关闭目录dir返回0,失败返回-1 */
int32_t sys_closedir(struct dir *dir)
{
int32_t ret = -1;
if (dir != NULL)
{
dir_close(dir);
ret = 0;
}
return ret;
}
代码很简单,就是对 dir_open 和 dir_close 进行调用,多了一些检查
k 遍历目录
获取一个目录项
/* 读取目录,成功返回1个目录项,失败返回NULL */
struct dir_entry *dir_read(struct dir *dir)
{
struct dir_entry *dir_e = (struct dir_entry *)dir->dir_buf;
struct inode *dir_inode = dir->inode;
uint32_t all_blocks[140] = {0}, block_cnt = 12;
uint32_t block_idx = 0, dir_entry_idx = 0;
while (block_idx < 12)
{
all_blocks[block_idx] = dir_inode->i_sectors[block_idx];
block_idx++;
}
if (dir_inode->i_sectors[12] != 0)
{ // 若含有一级间接块表
ide_read(cur_part->my_disk, dir_inode->i_sectors[12], all_blocks + 12, 1);
block_cnt = 140;
}
block_idx = 0;
uint32_t cur_dir_entry_pos = 0; // 当前目录项的偏移,此项用来判断是否是之前已经返回过的目录项
uint32_t dir_entry_size = cur_part->sb->dir_entry_size;
uint32_t dir_entrys_per_sec = SECTOR_SIZE / dir_entry_size; // 1扇区内可容纳的目录项个数
/* 因为此目录内可能删除了某些文件或子目录,所以要遍历所有块 */
while (block_idx < block_cnt)
{
if (dir->dir_pos >= dir_inode->i_size)
{
return NULL;
}
if (all_blocks[block_idx] == 0)
{ // 如果此块地址为0,即空块,继续读出下一块
block_idx++;
continue;
}
memset(dir_e, 0, SECTOR_SIZE);
ide_read(cur_part->my_disk, all_blocks[block_idx], dir_e, 1);
dir_entry_idx = 0;
/* 遍历扇区内所有目录项 */
while (dir_entry_idx < dir_entrys_per_sec)
{
if ((dir_e + dir_entry_idx)->f_type)
{ // 如果f_type不等于0,即不等于FT_UNKNOWN
/* 判断是不是最新的目录项,避免返回曾经已经返回过的目录项 */
if (cur_dir_entry_pos < dir->dir_pos)
{
cur_dir_entry_pos += dir_entry_size;
dir_entry_idx++;
continue;
}
ASSERT(cur_dir_entry_pos == dir->dir_pos);
dir->dir_pos += dir_entry_size; // 更新为新位置,即下一个返回的目录项地址
return dir_e + dir_entry_idx;
}
dir_entry_idx++;
}
block_idx++;
}
return NULL;
}
dir_read 用于根据传入的目录指针,一次返回一个该目录下的目录项,比如该目录文件下目录项分布:a,空,b,空,c,第一次调用返回 a,第二次调用返回 b,第三次调用返回 c…原理:目录内有个指向自己 inode 的指针,该 inode 内有 i_sectors[ ],所以能在磁盘中找到该目录文件,将其读出到缓冲区然后按需设定遍历规则即可。
代码中 dir_pos 实际含义是已经返回过的目录项总大小,cur_dir_entry_pos 的含义是此次调用遍历过程中,扫描过的非空目录项总大小。当扫描到的非空目录项时,此时若 cur_dir_entry_pos 与 dir_pos 相等,自然就可以判断出这个非空目录项应该返回。
封装
/* 读取目录dir的1个目录项,成功后返回其目录项地址,到目录尾时或出错时返回NULL */
struct dir_entry *sys_readdir(struct dir *dir)
{
ASSERT(dir != NULL);
return dir_read(dir);
}
/* 把目录dir的指针dir_pos置0 */
void sys_rewinddir(struct dir *dir)
{
dir->dir_pos = 0;
}
测试代码:
#include "print.h"
#include "init.h"
#include "fs.h"
#include "stdio.h"
#include "string.h"
#include "dir.h"
int main(void)
{
put_str("I am kernel\n");
init_all();
/******** 测试代码 ********/
struct dir *p_dir = sys_opendir("/dir1/subdir1");
if (p_dir)
{
printf("/dir1/subdir1 open done!\ncontent:\n");
char *type = NULL;
struct dir_entry *dir_e = NULL;
while ((dir_e = sys_readdir(p_dir)))
{
if (dir_e->f_type == FT_REGULAR)
{
type = "regular";
}
else
{
type = "directory";
}
printf(" %s %s\n", type, dir_e->filename);
}
if (sys_closedir(p_dir) == 0)
{
printf("/dir1/subdir1 close done!\n");
}
else
{
printf("/dir1/subdir1 close fail!\n");
}
}
else
{
printf("/dir1/subdir1 open fail!\n");
}
/******** 测试代码 ********/
while (1)
;
return 0;
}
l 删除目录
dir_is_empty
dir_remove
/* 判断目录是否为空 */
bool dir_is_empty(struct dir *dir)
{
struct inode *dir_inode = dir->inode;
/* 若目录下只有.和..这两个目录项则目录为空 */
return (dir_inode->i_size == cur_part->sb->dir_entry_size * 2);
}
/* 在父目录parent_dir中删除child_dir */
int32_t dir_remove(struct dir *parent_dir, struct dir *child_dir)
{
struct inode *child_dir_inode = child_dir->inode;
/* 空目录只在inode->i_sectors[0]中有扇区,其它扇区都应该为空 */
int32_t block_idx = 1;
while (block_idx < 13)
{
ASSERT(child_dir_inode->i_sectors[block_idx] == 0);
block_idx++;
}
void *io_buf = sys_malloc(SECTOR_SIZE * 2);
if (io_buf == NULL)
{
printk("dir_remove: malloc for io_buf failed\n");
return -1;
}
/* 在父目录parent_dir中删除子目录child_dir对应的目录项 */
delete_dir_entry(cur_part, parent_dir, child_dir_inode->i_no, io_buf);
/* 回收inode中i_secotrs中所占用的扇区,并同步inode_bitmap和block_bitmap */
inode_release(cur_part, child_dir_inode->i_no);
sys_free(io_buf);
return 0;
}
dir_is_empty 判断目录是否为空。原理:为空的目录对应的目录文件中只有两个目录项,分别是.和…。所以,我们直接判断目录对应的 inode 中的 i_size 是否等于两个目录项大小即可
dir_remove 传入父目录与子目录指针,在指定父目录中删除子目录。原理:判断子目录为空,然后调用 delete_dir_entry 删除父目录的目录文件中的子目录目录项,最后调用 inode_release 删除子目录的目录文件(子目录目录文件中只应该有.与…)
sys_rmdir
/* 删除空目录,成功时返回0,失败时返回-1*/
int32_t sys_rmdir(const char *pathname)
{
/* 先检查待删除的文件是否存在 */
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
int inode_no = search_file(pathname, &searched_record);
ASSERT(inode_no != 0);
int retval = -1; // 默认返回值
if (inode_no == -1)
{
printk("In %s, sub path %s not exist\n", pathname, searched_record.searched_path);
}
else
{
if (searched_record.file_type == FT_REGULAR)
{
printk("%s is regular file!\n", pathname);
}
else
{
struct dir *dir = dir_open(cur_part, inode_no);
if (!dir_is_empty(dir))
{ // 非空目录不可删除
printk("dir %s is not empty, it is not allowed to delete a nonempty directory!\n", pathname);
}
else
{
if (!dir_remove(searched_record.parent_dir, dir))
{
retval = 0;
}
}
dir_close(dir);
}
}
dir_close(searched_record.parent_dir);
return retval;
}
sys_rmdir 用于根据传入路径删除空目录。原理:先调用 search_file 查找这个路径对应目录的 inode,如果存在且该 inode 对应的确实是个目录文件,然后调用 dir_open 将该 inode 调入内存并创建对应的 struct dir,调用 dir_is_empty 判断该目录为空,最后调用 dir_remove 删除该目录。
m 工作目录
get_parent_dir_inode_nr
/* 获得父目录的inode编号 */
static uint32_t get_parent_dir_inode_nr(uint32_t child_inode_nr, void *io_buf)
{
struct inode *child_dir_inode = inode_open(cur_part, child_inode_nr);
/* 目录中的目录项".."中包括父目录inode编号,".."位于目录的第0块 */
uint32_t block_lba = child_dir_inode->i_sectors[0];
ASSERT(block_lba >= cur_part->sb->data_start_lba);
inode_close(child_dir_inode);
ide_read(cur_part->my_disk, block_lba, io_buf, 1);
struct dir_entry *dir_e = (struct dir_entry *)io_buf;
/* 第0个目录项是".",第1个目录项是".." */
ASSERT(dir_e[1].i_no < 4096 && dir_e[1].f_type == FT_DIRECTORY);
return dir_e[1].i_no; // 返回..即父目录的inode编号
}
get_parent_dir_inode_nr 传入子目录 inode 索引返回父目录的 inode 索引。原理:调用 inode_open 将子目录对应的 inode 加载到内存中,取出 inode 其中 i_sectors[0]地址,加载这个目录文件到内存中,找到…对应的目录项,从中取出父目录对应的 inode 索引返回即可。
get_child_dir_name
/* 在inode编号为p_inode_nr的目录中查找inode编号为c_inode_nr的子目录的名字,
* 将名字存入缓冲区path.成功返回0,失败返-1 */
static int get_child_dir_name(uint32_t p_inode_nr, uint32_t c_inode_nr, char *path, void *io_buf)
{
struct inode *parent_dir_inode = inode_open(cur_part, p_inode_nr);
/* 填充all_blocks,将该目录的所占扇区地址全部写入all_blocks */
uint8_t block_idx = 0;
uint32_t all_blocks[140] = {0}, block_cnt = 12;
while (block_idx < 12)
{
all_blocks[block_idx] = parent_dir_inode->i_sectors[block_idx];
block_idx++;
}
if (parent_dir_inode->i_sectors[12])
{ // 若包含了一级间接块表,将共读入all_blocks.
ide_read(cur_part->my_disk, parent_dir_inode->i_sectors[12], all_blocks + 12, 1);
block_cnt = 140;
}
inode_close(parent_dir_inode);
struct dir_entry *dir_e = (struct dir_entry *)io_buf;
uint32_t dir_entry_size = cur_part->sb->dir_entry_size;
uint32_t dir_entrys_per_sec = (512 / dir_entry_size);
block_idx = 0;
/* 遍历所有块 */
while (block_idx < block_cnt)
{
if (all_blocks[block_idx])
{ // 如果相应块不为空则读入相应块
ide_read(cur_part->my_disk, all_blocks[block_idx], io_buf, 1);
uint8_t dir_e_idx = 0;
/* 遍历每个目录项 */
while (dir_e_idx < dir_entrys_per_sec)
{
if ((dir_e + dir_e_idx)->i_no == c_inode_nr)
{
strcat(path, "/");
strcat(path, (dir_e + dir_e_idx)->filename);
return 0;
}
dir_e_idx++;
}
}
block_idx++;
}
return -1;
}
get_child_dir_name 通过传入的父目录 inode 索引与子目录 inode 索引,返回子目录的名字。原理:调用 inode_open 将父目录的 inode 载入内存,然后通过 inode 中的 i_sectors[ ]加载父目录的目录文件到内存中,遍历其中的目录项,遍历过程中比对目录项的 i_nr 是否与子目录的 inode 索引相等,如果是,则拷贝名字到缓冲区中。
然后在 pcb 中添加对应成员 cwd_inode_nr
,在初始化函数中初始化为 0
/* 把当前工作目录绝对路径写入buf, size是buf的大小.
当buf为NULL时,由操作系统分配存储工作路径的空间并返回地址
失败则返回NULL */
char *sys_getcwd(char *buf, uint32_t size)
{
/* 确保buf不为空,若用户进程提供的buf为NULL,
系统调用getcwd中要为用户进程通过malloc分配内存 */
ASSERT(buf != NULL);
void *io_buf = sys_malloc(SECTOR_SIZE);
if (io_buf == NULL)
{
return NULL;
}
struct task_struct *cur_thread = running_thread();
int32_t parent_inode_nr = 0;
int32_t child_inode_nr = cur_thread->cwd_inode_nr;
ASSERT(child_inode_nr >= 0 && child_inode_nr < 4096); // 最大支持4096个inode
/* 若当前目录是根目录,直接返回'/' */
if (child_inode_nr == 0)
{
buf[0] = '/';
buf[1] = 0;
return buf;
}
memset(buf, 0, size);
char full_path_reverse[MAX_PATH_LEN] = {0}; // 用来做全路径缓冲区
/* 从下往上逐层找父目录,直到找到根目录为止.
* 当child_inode_nr为根目录的inode编号(0)时停止,
* 即已经查看完根目录中的目录项 */
while ((child_inode_nr))
{
parent_inode_nr = get_parent_dir_inode_nr(child_inode_nr, io_buf);
if (get_child_dir_name(parent_inode_nr, child_inode_nr, full_path_reverse, io_buf) == -1)
{ // 或未找到名字,失败退出
sys_free(io_buf);
return NULL;
}
child_inode_nr = parent_inode_nr;
}
ASSERT(strlen(full_path_reverse) <= size);
/* 至此full_path_reverse中的路径是反着的,
* 即子目录在前(左),父目录在后(右) ,
* 现将full_path_reverse中的路径反置 */
char *last_slash; // 用于记录字符串中最后一个斜杠地址
while ((last_slash = strrchr(full_path_reverse, '/')))
{
uint16_t len = strlen(buf);
strcpy(buf + len, last_slash);
/* 在full_path_reverse中添加结束字符,做为下一次执行strcpy中last_slash的边界 */
*last_slash = 0;
}
sys_free(io_buf);
return buf;
}
/* 更改当前工作目录为绝对路径path,成功则返回0,失败返回-1 */
int32_t sys_chdir(const char *path)
{
int32_t ret = -1;
struct path_search_record searched_record;
memset(&searched_record, 0, sizeof(struct path_search_record));
int inode_no = search_file(path, &searched_record);
if (inode_no != -1)
{
if (searched_record.file_type == FT_DIRECTORY)
{
running_thread()->cwd_inode_nr = inode_no;
ret = 0;
}
else
{
printk("sys_chdir: %s is regular file or other!\n", path);
}
}
dir_close(searched_record.parent_dir);
return ret;
}
sys_getcwd 用于解析当前正在运行进程或线程的绝对工作路径。原理:我们之前已经为 PCB 中添加了 cwd_inode_nr 用于表示任务工作目录 inode 索引,假设现在这个 inode 索引是正确的。首先调用 get_parent_dir_inode_nr 得到父目录 inode 索引,此时原来的 cwd_inode_nr 就变成了子目录 inode 索引,然后就可以调用 get_child_dir_name 得到子目录名称,然后将父目录 inode 索引转换成新的子目录索引,又调用 get_parent_dir_inode_nr 得到新的父目录索引…如此循环,缓冲区中就会存储反转的绝对路径。比如一个进程工作在/home/kanshan/test 下,缓冲区就会存入/test/kanshan/home,所以我们最后把这个路径反转过来即可。
sys_chdir
传入一个目录的路径,然后改变当前正在运行的进程或线程的工作目录索引。原理:调用 search_file
将传入路径解析成一个对应的 inode 索引,然后直接去修改当前任务 pcb 的 cwd_inode_nr 为之前返回的 inode 索引就行了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具