绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
[文件系统] 一个简单文件系统的实现(6)
2020-05-21 16:32:18

接着写超级块操作
接下来就是gt_write_super(定义在super.c中)  

static int gt_write_super(struct super_block *sb){
struct gt_super_block *gs;
lock_kernel();
gs=GT_SB(sb)->s_gs;
gs->s_free_blocks_count=cpu_to_le32(gt_count_free_blocks(sb));
gs->s_free_inodes_count=cpu_to_le32(gt_count_free_inodes(sb));
gs->s_mtime=cpu_to_le32(get_seconds());
gs->s_wtime = cpu_to_le32(get_seconds());
mark_buffer_dirty(GT_SB(sb)->s_sbh);
sync_dirty_buffer(GT_SB(sb)->s_sbh);
sb->s_dirt=;
unlock_kernel();
}

这个很简单很简单,简单到我都懒的讲了..
让我们来看看gt_count_free_blocks和gt_count_free_inodes
这俩函数都定义在inode.c中

unsigned long gt_count_free_inodes(struct super_block *sb){
struct buffer_head *bh;
struct gt_inode *gt;
char *p;

unsigned long block=2; //索引节点表所在块
unsigned long count=;//使用了的索引节点数
//然后遍历索引节点表
while(bh=sb_bread(sb,block)){
p=bh->b_data;
while(p<=(bh->b_data+GT_BLOCK_SIZE-GT_INODE_SIZE)){
gt=(struct gt_inode *)p;
if(gt->i_nlinks)
count++;//已经使用的索引节点数加一
p+=GT_INODE_SIZE;
}
brelse(bh);
if(block>GT_INODE_BLOCK_COUNT(sb))//如果到了索引节点表结尾则跳出
break;
block++;
}

return GT_SB(sb)->s_gs->s_inodes_count-count;//返回未使用的索引节点数
}
unsigned long gt_count_free_blocks(struct super_block *sb){

struct gt_super_block *gs;
char *p;
int block=2;
gs=GT_SB(sb)->s_gs;
unsigned long used=;//已经使用的块数
struct buffer_head *bh;
struct gt_inode * gt;
//遍历索引节点表,已经使用的块数其实就等于后一个索引节点的i_end_block
while(bh=sb_bread(sb,block)){
p=bh->b_data;
while(p<=(bh->b_data+GT_BLOCK_SIZE-GT_INODE_SIZE)){
gt=(struct gt_inode *)p;
if(!gt->i_blocks)
used=gt->i_end_block;

}
brelse(bh);
}
return GT_BLOCKS(sb)-used;
}


文章来源CU社区:[文件系统] 一个简单文件系统的实现

分享好友

分享这个小栈给你的朋友们,一起进步吧。

内核源码
创建时间:2020-05-18 13:36:55
内核源码精华帖内容汇总
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~