笔记019 - HW10: bigger files for xv6

题意

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
2
3
4
Makefile

CPUS := 1
QEMUOPTS : 添加 -snapshot
1
下载big.c,添加到Makefile的UPROGS。
1
2
3
4
param.h 修改FSSIZE

//#define FSSIZE 1000 // size of file system in blocks
#define FSSIZE 21113 // size of file system in blocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fs.h 修改NDIRECT、MAXFILE、dinode.addrs,添加NDBINDIRECT

//#define NDIRECT 12
#define NDIRECT 11
#define NDBINDIRECT (NINDIRECT * NINDIRECT)

#define NINDIRECT (BSIZE / sizeof(uint)) // 512/4=128
//#define MAXFILE (NDIRECT + NINDIRECT)
#define MAXFILE (NDIRECT + NINDIRECT +NDBINDIRECT)

// 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+1]; // Data block addresses
uint addrs[NDIRECT+2]; // Data block addresses
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
file.h 修改inode.addrs

struct inode {
uint dev; // Device number
uint inum; // Inode number
int ref; // Reference count
struct sleeplock lock;
int flags; // I_VALID

short type; // copy of disk inode
short major;
short minor;
short nlink;
uint size;
//uint addrs[NDIRECT+1];
uint addrs[NDIRECT+2];
};

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”);
}
`

显示 Gitment 评论