首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XV6试图在xv6操作系统中实现三重间接方向时崩溃

XV6试图在xv6操作系统中实现三重间接方向时崩溃
EN

Stack Overflow用户
提问于 2015-06-15 19:37:44
回答 1查看 2.3K关注 0票数 1

最初的xv6-rev7操作系统包含:

12个定向块

1个间接blcok(指向128个区块)

也就是说我们有140个街区。

每个块的大小是512 in,==> 512* 140 = 71,680 ~= 70 in是xv6中文件大小的限制。

我希望在xv6中实现三重间接访问,以便支持大小为40 of的文件。

为了做到这一点,我需要在三重间接之前实现双间接。

所以我从我的12个街区中拿出了2个定向街区。

1为双间接,另1为三间接。

这就是我现在拥有的:

直接:10个街区

单一间接: 128

双重间接: 128*128

三重间接: 4* 128 *128 (我使用的是4而不是128,因为这足以容纳40 am )

这就是为什么#define NDIRECT 10uint addrs[NDIRECT+3];

文件大小限制= (10 + 128 + 128*128 +4*128*128)*512 10= 42,013,696 ~= 42 10

所以我理解这个概念。三重间接的实现是在文件bmap中的fs.c函数中实现的.

这就是它的样子:

由于某些原因,当我试图创建大小为8.5MB的文件时,它会失败:

我正在使用bochs仿真器

我也不确定在mkfs.c中需要更改哪些值:

代码语言:javascript
复制
int nblocks = 20985;
int nlog = LOGSIZE;
int ninodes = 200;
int size = 21029;

fs.h:

代码语言:javascript
复制
// On-disk file system format. 
// Both the kernel and user programs use this header file.

// Block 0 is unused.
// Block 1 is super block.
// Blocks 2 through sb.ninodes/IPB hold inodes.
// Then free bitmap blocks holding sb.size bits.
// Then sb.nblocks data blocks.
// Then sb.nlog log blocks.

#define ROOTINO 1  // root i-number
#define BSIZE 512  // block size

// File system super block
struct superblock {
  uint size;         // Size of file system image (blocks)
  uint nblocks;      // Number of data blocks
  uint ninodes;      // Number of inodes.
  uint nlog;         // Number of log blocks
};

#define NDIRECT 10
#define NINDIRECT (BSIZE / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT + NINDIRECT*NINDIRECT + 4*NINDIRECT*NINDIRECT)

// 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+3];   // Data block addresses
};

// Inodes per block.
#define IPB           (BSIZE / sizeof(struct dinode))

// Block containing inode i
#define IBLOCK(i)     ((i) / IPB + 2)

// Bitmap bits per block
#define BPB           (BSIZE*8)

// Block containing bit for block b
#define BBLOCK(b, ninodes) (b/BPB + (ninodes)/IPB + 3)

// Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14

struct dirent {
  ushort inum;
  char name[DIRSIZ];
};

fs.c:

代码语言:javascript
复制
// Return the disk block address of the nth block in inode ip.
// If there is no such block, bmap allocates one.
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;
  }

  /* Double indirect */
  bn -= NINDIRECT;
  if(bn < NINDIRECT*NINDIRECT){
        // Load 2nd indirect block, allocating if necessary.
        if((addr = ip->addrs[NDIRECT+1]) == 0) // 2d block. NDIRECT+1 is to get the index vector
          ip->addrs[NDIRECT+1] = addr = balloc(ip->dev);

        bp = bread(ip->dev, addr);
        a = (uint*)bp->data;
        if ((addr = a[bn/(NINDIRECT)]) == 0) { /* get index for 1st
                                                    indirection. (NINDIRECT is 128) */
              a[bn/(NINDIRECT)] = addr = balloc(ip->dev);
              log_write(bp);
          }
          brelse(bp);               /* release the double indirect table
                                       (main level) */

        bp = bread(ip->dev, addr);
        a = (uint*)bp->data;

         if ((addr = a[bn%(NINDIRECT)]) == 0) { /*  get the 2nd level table */
              a[bn%(NINDIRECT)] = addr = balloc(ip->dev);
              log_write(bp);
          }

        brelse(bp);
        return addr;
    }

   /* Triple indirect */

      bn -= NINDIRECT*NINDIRECT;
      if(bn < 4*NINDIRECT*NINDIRECT){
        // Load 3rd indirect block, allocating if necessary.
        if((addr = ip->addrs[NDIRECT+2]) == 0) // 3d block. NDIRECT+2 is to get the index vector
          ip->addrs[NDIRECT+2] = addr = balloc(ip->dev);

        bp = bread(ip->dev, addr);
        a = (uint*)bp->data;

        if ((addr = a[bn/(NINDIRECT*4)]) == 0) { /* get index for 2st
                                                    indirection. (NINDIRECT is 128) */
              a[bn/(NINDIRECT*4)] = addr = balloc(ip->dev);
              log_write(bp);
          }
          brelse(bp);

        bp = bread(ip->dev, addr);
        a = (uint*)bp->data;

        if ((addr = a[bn/(NINDIRECT*NINDIRECT*4)]) == 0) { 

              a[bn/(NINDIRECT*NINDIRECT*4)] = addr = balloc(ip->dev);
              log_write(bp);
          }

          brelse(bp);               


         if ((addr = a[bn%(NINDIRECT*NINDIRECT*4)]) == 0) { 
              a[bn%(NINDIRECT*NINDIRECT*4)] = addr = balloc(ip->dev);
              log_write(bp);
          }

        brelse(bp);
        return addr;
    }

  panic("bmap: out of range");
}

mkfs.c:

代码语言:javascript
复制
#define stat xv6_stat  // avoid clash with host struct stat
#include "types.h"
#include "fs.h"
#include "stat.h"
#include "param.h"

int nblocks = 20985;
int nlog = LOGSIZE;
int ninodes = 200;
int size = 21029;

bigfile.c:

代码语言:javascript
复制
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
void
help()
{
  printf(1, "usage:\nfiles <name> <letter> <num>\n"
            "e.g. nfiles foo a 40\n creates a file foo, with 40 times the letter a\n");
}

void
num2str(int i, char str[3])
{
  str[2]=i%10+'0';
  i=i/10;
  str[1]=i%10+'0';
  i=i/10;
  str[0]=i%10+'0';
  i=i/10;
}
#define BUF_SZ 512

int
main(int argc, char *argv[])
{
    int i, count, fd, n;
    // char *name;
    // char c;
    char buf[BUF_SZ];
    if (argc !=4) {
        help();
        exit();
    }
    count = atoi(argv[3]);
    if((fd=open(argv[1], O_CREATE|O_RDWR))<0) {
        printf(2,"Failed to open file: %s\n", argv[1]);
        exit();
    }
    for (i=0; i<BUF_SZ;i++)
        buf[i]=argv[2][0];
    for (i=0; i<count/BUF_SZ;i++)
        if ((n=write(fd,buf,BUF_SZ)) != BUF_SZ)
        {
            printf(2,"Failed 1 to Write count=%d\n",i*BUF_SZ);
            exit();
        }

    for (i=0; i<count%BUF_SZ;i++)
        if ((n=write(fd,argv[2],1)) != 1)
        {
            printf(2,"Failed 2 to Write count=%d\n",count-i);
            exit();
        }

  exit();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-16 21:16:28

1.在mkfs.c中定义的n块数量不足。

代码语言:javascript
复制
int nblocks = 20985;
int nlog = LOGSIZE;
int ninodes = 200;
int size = 21029;

你的定义是:

代码语言:javascript
复制
#define MAXFILE (NDIRECT + NINDIRECT + NINDIRECT*NINDIRECT + 4*NINDIRECT*NINDIRECT

它等于: 10+128+128^2+4*128^2 = 82058。

只需选择一些大于82058的nblocks,并相应地更新size

2.在bmap()函数中,在三重间接代码中,您的第一级间接方向是一个四项数组(正如您在图表中提到的)。一旦您知道您应该访问这四个条目中的哪一个,您就返回了双重间接问题,您已经解决了这个问题。

因此,为了知道您应该访问的四个条目中的哪一个,可以使用以下命令:

代码语言:javascript
复制
if((addr = a[bn/(NINDIRECT*NINDIRECT)]) == 0){
  a[bn/(NINDIRECT*NINDIRECT)] = addr = balloc(ip->dev);
  log_write(bp);
}

然后,您可以这样减少bn

代码语言:javascript
复制
bn -= ((NINDIRECT*NINDIRECT)*(bn/(NINDIRECT*NINDIRECT)));

再次解决双重间接问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30853617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档