MIT_6.828_2018_Homework:bigger files for xv6

地址: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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值