题意
xv6的inode包括12个直接的block number和1个间接的block,间接的block存储128个block number,最大支持的文件长度为12+128=140个sectors。
修改xv6,将其中一个block number修改为双层间接的block,使得最大支持的文件长度为11+128+128x128=16523个sectors。
解决思路
1 | Makefile |
1 | 下载big.c,添加到Makefile的UPROGS。 |
1 | param.h 修改FSSIZE |
1 | fs.h 修改NDIRECT、MAXFILE、dinode.addrs,添加NDBINDIRECT |
1 | file.h 修改inode.addrs |
fs.c的bmap()函数负责根据逻辑block number返回对应的物理block(若对应的记录为空,则先分配)。原先的bmap()函数先检查逻辑block number是否在NDIRECT范围内(直接的block);否则,逻辑block number减去NDIRECT,检查新的逻辑block number是否在NINDIRECT范围内(间接的block)。新添加的处理过程是:逻辑block number再减去NINDIRECT,检查新的逻辑block number是否在NDBINDIRECT范围内(双层间接的block)。如果符合要求,则根据需要获取间接block内容或分配最终的block。
xv6 inode的标准格式如下:
扩展后的xv6 inode的格式如下:
编译并运行:1
2
3
4
5
6
7$ make qemu
$ big
```
{% asset_img 3.JPG 编译并运行 %}
可以看到,系统支持的size和nblocks都更新了,支持的最大文件长度为16523个sectors。
# 源代码
//fs.c
static uint
bmap(struct inode ip, uint bn)
{
uint addr, a;
struct buf *bp;
if(bn < NDIRECT){
if((addr = ip->addrs[bn]) == 0)
ip->addrs[bn] = addr = balloc(ip->dev);
return addr;
}
bn -= NDIRECT;
if(bn < NINDIRECT){
// Load indirect block, allocating if necessary.
if((addr = ip->addrs[NDIRECT]) == 0)
ip->addrs[NDIRECT] = addr = balloc(ip->dev);
bp = bread(ip->dev, addr);
a = (uint*)bp->data;
if((addr = a[bn]) == 0){
a[bn] = addr = balloc(ip->dev);
log_write(bp);
}
brelse(bp);
return addr;
}
//homework10
bn -= NINDIRECT;
if(bn < NDBINDIRECT){
if((addr = ip->addrs[NINDIRECT+1]) == 0)
ip->addrs[NINDIRECT+1] = addr = balloc(ip->dev);
bp = bread(ip->dev, addr);
a = (uint*)bp->data;
int first_block_index = bn/NINDIRECT;
if((addr = a[first_block_index]) == 0){
a[first_block_index] = addr = balloc(ip->dev);
log_write(bp);
}
brelse(bp);
bp = bread(ip->dev, addr);
a = (uint*)bp->data;
int second_block_index = bn%NINDIRECT;
if((addr = a[second_block_index]) == 0){
a[second_block_index] = addr = balloc(ip->dev);
log_write(bp);
}
brelse(bp);
return addr;
}
panic(“bmap: out of range”);
}`