在块分配机制中,涉及到几个主要的数据结构。
通过ext4_allocation_request描述块请求,然后基于块查找结果即上层需求来决定是否执行块分配操作。
在分配过程中,为了更好执行分配,记录一些信息,需要对分配行为进行描述,就有结构体ext4_allocation_contex。
在搜寻可用空间过程中,是有可能使用预分配空间的,因此还需要有能够描述预分配空间大小等属性的描述符ext4_prealloc_space。
下面,对各个关键结构体进行详细的分析。
1. 块请求描述符ext4_allocation_request
块分配请求属性,有请求描述符ext4_allocation_request来描述:
structext4_allocation_request {
/* target inode for block we'reallocating */
struct inode *inode;
/* how many blocks we want to allocate*/
unsigned int len;
/* logical block in target inode */
ext4_lblk_t logical;
/* the closest logical allocated blockto the left */
ext4_lblk_t lleft;
/* the closest logical allocated blockto the right */
ext4_lblk_t lright;
/* phys. target (a hint) */
ext4_fsblk_t goal;
/* phys. block for the closest logicalallocated block to the left */
ext4_fsblk_t pleft;
/* phys. block for the closest logicalallocated block to the right */
ext4_fsblk_t pright;
/* flags. see above EXT4_MB_HINT_* */
unsigned int flags;
};
这个请求描述符结构体在ext4_ext_map_blocks()中初始化(注:ext4_ext_map_blocks()的作用是查找或分配指定的block块,并完成与缓存空间的映射)。
具体上述信息也就一个成员变量goal值的我们分析一下,goal记录是物理块号,其隐含含义比较重要:goal虽然只是记录物理块号,但是这个物理块号的选择可以很大程度的是文件保证locality特性及其物理地址连续性。
goal是由函数ext4_ext_find_goal()来定义:
static ext4_fsblk_t ext4_ext_find_goal(struct inode*inode,
struct ext4_ext_path *path,
ext4_lblk_t block)
{
if(path) {
intdepth = path->p_depth;
structext4_extent *ex;
/*
* Try to predict block placement assuming thatwe are
* filling in a file which will eventually be
* non-sparse --- i.e., in the case of libbfdwriting
* an ELF object sections out-of-order but in away
* the eventually results in a contiguousobject or
* executable file, or some database extendinga table
* space file. However, this is actually somewhat
* non-ideal if we are writing a sparse filesuch as
* qemu or KVM writing a raw image file that isgoing
* to stay fairly sparse, since it will end up
* fragmenting the file system's free space. Maybe we
* should have some hueristics or some way toallow
* userspace to pass a hint to file system,
* especially if the latter case turns out tobe
* common.
*/
ex= path[depth].p_ext;
if(ex) {
ext4_fsblk_text_pblk = ext4_ext_pblock(ex);
ext4_lblk_text_block = le32_to_cpu(ex->ee_block);
if(block > ext_block)
returnext_pblk + (block - ext_block);
else
returnext_pblk - (ext_block - block);
}
/*it looks like index is empty;
* try to find starting block from index itself*/
if(path[depth].p_bh)
returnpath[depth].p_bh->b_blocknr;
}
/*OK. use inode's group */
returnext4_inode_to_goal_block(inode);
}
细细分析这段代码,如果从根目录到指定逻辑块的path存在,那么就需要根据path来计算目标物理块的地址。
(1) Path的终点若是dataextent,则说明该path是从根到叶子的。当请求block号大于path叶子extent的起始逻辑块号ext_block (对应物理块号为pblk),其逻辑块的距离为(block-ext_block),为在最可能上保证对应物理地址的连续性;只需返回与pblk+(block-ext_block)物理块号最接近的空闲物理块即可;而对于请求block号小于extent的起始逻辑块号ext_block的情况,只需尽最可能以pblk-( ext_block -block)物理块号为目标寻找与其物理地址最接近的空闲物理块即可。因此,我们指定goal分别为pblk+(block-ext_block)和pblk-(block-ext_block)。
(2) 而如果path存在,却没有叶子,那则么办,很简单,我们只需要将goal物理块号指定为最后一个的extent block对应的物理块号既可。
(3) 还有一种情况,没有给出path。个人认为,这种场景即inode刚create的情况。有专门的ext4_inode_to_goal_block()来实现:
ext4_fsblk_t ext4_inode_to_goal_block(struct inode*inode)
{
structext4_inode_info *ei = EXT4_I(inode);
ext4_group_tblock_group;
ext4_grpblk_tcolour;
intflex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
ext4_fsblk_tbg_start;
ext4_fsblk_tlast_block;
block_group= ei->i_block_group;
if(flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
/*
* If there are at leastEXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
* block groups per flexgroup, reserve thefirst block
* group for directories and special files. Regular
* files will start at the second blockgroup. This
* tends to speed up directory access andimproves
* fsck times.
*/
block_group&= ~(flex_size-1);
if(S_ISREG(inode->i_mode))
block_group++;
}
bg_start= ext4_group_first_block_no(inode->i_sb, block_group);
last_block= ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
/*
* If we are doing delayed allocation, we don'tneed take
* colour into account.
*/
if(test_opt(inode->i_sb, DELALLOC))
returnbg_start;
if(bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
colour= (current->pid % 16) *
(EXT4_BLOCKS_PER_GROUP(inode->i_sb)/ 16);
else
colour= (current->pid % 16) * ((last_block - bg_start) / 16);
returnbg_start + colour;
}