地址:bigger files for xv6
题目要求:原来的一个文件的最大大小为12+128=140个blocks,现在将前12个直接块取一个出来存放另一个“doubly-indirect”块,这样就新增了128*128-1个块了。有点类似与计算机组成原理里面的存储器间接寻址。
准备工作
首先修改Makefile:
CPUS := 1
加入
QEMUEXTRA = -snapshot
修改param.h:
#define FSSIZE 20000 // size of file system in blocks
开始
修改fs.h
修改NDIRECT:
#define NDIRECT 11
#define MAXFILE (NDIRECT + NINDIRECT + NINDIRECT*NINDIRECT)
同时修改dinode中addrs定义:
// On-disk inode structure
struct dinode {
short type; // File type
short major; // Major device number (T_DEV only)
short minor; // Minor device number (T_DEV only)
short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes)
uint addrs[NDIRECT+2]; // Data block addresses
};
最后修改fs.c中的bmap()函数:
static uint
bmap(struct inode *ip, uint bn)
{
uint addr, *a;
struct buf *bp;
struct buf *dp;
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;
}
bn -= NINDIRECT;
uint DBNINDIRECT = NINDIRECT*NINDIRECT;
if (bn < DBNINDIRECT)
{
//在第一级中的位置
uint which_block = bn/NINDIRECT;
//在第二级中的位置
uint final_location = bn - which_block*NINDIRECT;
if ((addr = ip->addrs[NDIRECT+1]) == 0)
ip->addrs[NDIRECT+1] = addr = balloc(ip->dev);
bp = bread(ip->dev,addr);
a = (uint*)bp->data;
if ((addr= a[which_block]) == 0)
{
a[which_block] = addr = balloc(ip->dev);
log_write(bp);
}
dp = bread(ip->dev,addr);
a = (uint*)dp->data;
if ((addr = a[final_location]) == 0)
{
a[final_location] = addr = balloc(ip->dev);
log_write(dp);
}
brelse(bp);
brelse(dp);
return addr;
}
panic("bmap: out of range");
}
运行结果
运行make;make qemu
,在shell里面输入big,得到如下结果:
qemu -serial mon:stdio -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp 1 -m 512 -snapshot
xv6...
cpu0: starting 0
sb: size 20000 nblocks 19937 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58
init: starting sh
$ big
.....................................................................................................................................................................
wrote 16523 sectors
总结:注意体会xv6中的分层思想,各个层的作用。
END.