[文件系统] 一个简单文件系统的实现(6)
接着写超级块操作
接下来就是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社区:[文件系统] 一个简单文件系统的实现
相关文章