yaffs 文件系统的位图如下所示:
每个block都会在chunk_bits数组中占几个字节, 字节每个bit指代了该block上的chunk的分配状态(0-示分配; 1-己分配)
1. 初始化
yaffs_guts_initialise
-->
yaffs_init_blocks
-
static int yaffs_init_blocks(struct yaffs_dev *dev)
-
{
-
//chunks_per_block=128, 1个chunk 1个bit,那么128个chunk需要用16B来表示
-
dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
-
dev->chunk_bits = kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS); //所以chunk_bits数组需要16B*n_blocks
-
if (!dev->chunk_bits) {
-
dev->chunk_bits = vmalloc(dev->chunk_bit_stride * n_blocks);
-
dev->chunk_bits_alt = 1;
-
} else {
-
dev->chunk_bits_alt = 0;
-
}
-
if (!dev->chunk_bits)
-
goto alloc_error;
-
-
memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
-
memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
-
return YAFFS_OK;
-
}
2. 设置
当调用yaffs_alloc_chunk分配一个chunk时就会在位图数组中设置一位,标识着该chunk己被占用
-
yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
-
{
-
//参数: dev
-
// dev->alloc_block: 要分配chunk的block
-
// dev->alloc_page: 要分配的chunk号
-
//返回: 当前block在chunk_bits数组中的地址
-
u8 *blk_bits = yaffs_block_bits(dev, blk);
-
{
-
if (blk < dev->internal_start_block || blk > dev->internal_end_block)
-
;
-
//yaffs_init_blocks中赋值:
-
//dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
-
return dev->chunk_bits + (dev->chunk_bit_stride * (blk - dev->internal_start_block)); //获取当前block在位图数组的首地址
-
}
-
-
yaffs_verify_chunk_bit_id(dev, blk, chunk);
-
//此处chunk: 当前block上的第几个chunk
-
blk_bits[chunk / 8] |= (1 << (chunk & 7)); //chunk/8指该chunk在block所占用的位图的第几个字节上,将这个字节的chunk_bit置位,表示己被占用
-
}
注意: (u8 *)dev->chunk_bits: 位图数组的首地址
(int)dev->chunk_bit_stride: 每个block在位图中占用了几个字节,它是在yaffs_init_blocks中赋值的:dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
3. 在位图中取消一个chunk
-
void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
-
{
-
u8 *blk_bits = yaffs_block_bits(dev, blk);
-
-
yaffs_verify_chunk_bit_id(dev, blk, chunk);
-
blk_bits[chunk / 8] &= ~(1 << (chunk & 7)); //知道了设置,那么释放很自己就是将这一位清0了
-
}
4. 批量操作
-
void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
-
{
-
u8 *blk_bits = yaffs_block_bits(dev, blk);
-
-
memset(blk_bits, 0, dev->chunk_bit_stride); //将整个block所占位图中的Bytes都清0
-
}
阅读(2123) | 评论(0) | 转发(2) |