Lab: file system

XV6 Lab8:File System 符号链接类似于硬链接,但硬链接仅限于指向同一磁盘上的文件,而符号链接可以跨磁盘设备。增加xv6系统中文件的最大长度,创建一个大小为65803 blocks的文件(发现正好是11+256+256*256,即减少一个直接映射块,修改为二级映射即可)如果标志位O_NOFOLLOW为false,系统调用open应该递归,找到不是软链接的文件将其打开,需要设置一个递归深度上限防止链接的循环。,该标志可用于系统调用open(请注意,传递给open的标志使用位运算符OR,因此新标志不应与任何现有标志重叠) 阅读详情

Lab: file system

Large files (moderate)

修改bmap使得inode支持更大的块。Currently xv6 files are limited to 268 blocks, or 268BSIZE bytes (BSIZE is 1024 in xv6). This limit comes from the fact that an xv6 inode contains 12 “direct” block numbers and one “singly-indirect” block number, which refers to a block that holds up to 256 more block numbers, for a total of 12+256=268 blocks.将一个direct block number用作"doubly-indirect" block, containing 256 addresses of singly-indirect blocks, each of which can contain up to 256 addresses of data blocks. The result will be that a file will be able to consist of up to 65803 blocks, or 256256+256+11 blocks (11 instead of 12, because we will sacrifice one of the direct block numbers for the double-indirect block).
Your Job
Modify bmap() so that it implements a doubly-indirect block, in addition to direct blocks and a singly-indirect block. You’ll have to have only 11 direct blocks, rather than 12, to make room for your new doubly-indirect block; you’re not allowed to change the size of an on-disk inode. The first 11 elements of ip->addrs[] should be direct blocks; the 12th should be a singly-indirect block (just like the current one); the 13th should be your new doubly-indirect block. You are done with this exercise when bigfile writes 65803 blocks and usertests -q runs successfully:

修改头文件

fs.h
#define NDIRECT 11
#define NINDIRECT (BSIZE / sizeof(uint))
#define SECINDIRECT (NINDIRECT * NINDIRECT)
#define MAXFILE (NDIRECT + NINDIRECT + SECINDIRECT)

// On-disk inode structure
struct dinode {
  short type;           // File type
  short major;          // Major device number (T_DEVICE only)
  short minor;          // Minor device number (T_DEVICE only)
  short nlink;          // Number of links to inode in file system
  uint size;            // Size of file (bytes)
  uint addrs[NDIRECT+2];   // Data block addresses
};

file.h
// in-memory copy of an inode
struct inode {
  uint dev;           // Device number
  uint inum;          // Inode number
  int ref;            // Reference count
  struct sleeplock lock; // protects everything below here
  int valid;          // inode has been read from disk?

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

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){
      addr = balloc(ip->dev);
      if(addr == 0)
        return 0;
      ip->addrs[bn] = addr;
    }
    return addr;
  }
  bn -= NDIRECT;

  if(bn < NINDIRECT){
    // Load indirect block, allocating if necessary.
    if((addr = ip->addrs[NDIRECT]) == 0){
      addr = balloc(ip->dev);
      if(addr == 0)
        return 0;
      ip->addrs[NDIRECT] = addr;
    }
    bp = bread(ip->dev, addr);
    a = (uint*)bp->data;
    if((addr = a[bn]) == 0){
      addr = balloc(ip->dev);
      if(addr){
        a[bn] = addr;
        log_write(bp);
      }
    }
    brelse(bp);
    return addr;
  }
  bn -= NINDIRECT;
  if(bn < SECINDIRECT){
    // Load second indirect block, allocating if necessary.
    int first = bn / NINDIRECT, second = bn % NINDIRECT;
    if((addr = ip->addrs[NDIRECT+1]) == 0){
      addr = balloc(ip->dev);
      if(addr == 0)
        panic("bmap: out of blocks");
      ip->addrs[NDIRECT+1] = addr;
    }
    bp = bread(ip->dev, addr);
    a = (uint*)bp->data;
    if((addr = a[first]) == 0){
      addr = balloc(ip->dev);
      if(addr == 0)
        return 0;
      a[first] = addr;
      log_write(bp);
    }
    brelse(bp);
    bp = bread(ip->dev, addr);
    a = (uint*)bp->data;
    if((addr = a[second]) == 0){
      addr = balloc(ip->dev);
      if(addr == 0)
        return 0;
      a[second] = addr;
      log_write(bp);
    }
    brelse(bp);
    return addr;
  }
  panic("bmap: out of range");
}

void
itrunc(struct inode *ip)
{
  int i, j;
  struct buf *bp;
  uint *a;

  for(i = 0; i < NDIRECT; i++){
    if(ip->addrs[i]){
      bfree(ip->dev, ip->addrs[i]);
      ip->addrs[i] = 0;
    }
  }

  if(ip->addrs[NDIRECT]){
    bp = bread(ip->dev, ip->addrs[NDIRECT]);
    a = (uint*)bp->data;
    for(j = 0; j < NINDIRECT; j++){
      if(a[j])
        bfree(ip->dev, a[j]);
    }
    brelse(bp);
    bfree(ip->dev, ip->addrs[NDIRECT]);
    ip->addrs[NDIRECT] = 0;
  }

  if(ip->addrs[NDIRECT+1]){
    bp = bread(ip->dev, ip->addrs[NDIRECT+1]);
    a = (uint*)bp->data;
    for(i = 0; i < NINDIRECT; i++){
      if(a[i]){
        struct buf *bp1 = bread(ip->dev, a[i]);
        uint *b = (uint*)bp1->data;
        for(j = 0; j < NINDIRECT; j++){
          if(b[j])
            bfree(ip->dev, b[j]);
        }
        brelse(bp1);
        bfree(ip->dev, a[i]);
      }
    }
    brelse(bp);
    bfree(ip->dev, ip->addrs[NDIRECT+1]);
    ip->addrs[NDIRECT+1] = 0;
  }
  ip->size = 0;
  iupdate(ip);
}

结果在usertests中的textwrite这一项无法通过
查看代码

    volatile int *addr = (int *) 0;
    *addr = 10;
    exit(1);

cpu执行应该会引发中断,但是并没有。同样的代码放在xv6 2024中就正确的引发了中断。

Symbolic links (moderate)

In this exercise you will add symbolic links to xv6. Symbolic links (or soft links) refer to a linked file by pathname; when a symbolic link is opened, the kernel follows the link to the referred file. Symbolic links resembles hard links, but hard links are restricted to pointing to file on the same disk, while symbolic links can cross disk devices. Although xv6 doesn’t support multiple devices, implementing this system call is a good exercise to understand how pathname lookup works.

Your job
You will implement the symlink(char *target, char *path) system call, which creates a new symbolic link at path that refers to file named by target. For further information, see the man page symlink. To test, add symlinktest to the Makefile and run it. Your solution is complete when the tests produce the following output (including usertests succeeding).

struct inode*
follow_symlink(struct inode *ip, int depth) {
  if(depth >= 10) {  // 最大递归深度为10
    return 0;       // 可能存在循环链接
  }
  
  char next[MAXPATH];
  int link_len = readi(ip, 0, (uint64)next, 0, MAXPATH);
  if(link_len <= 0) {
    return 0;
  }
  next[link_len] = 0;
  
  iunlockput(ip);

  if((ip = namei(next))== 0) {
    return 0;
  }
  ilock(ip);

  if(ip->type == T_SYMLINK) {
    return follow_symlink(ip, depth + 1);
  }
  return ip;
}

uint64
sys_open(void)
{
  char path[MAXPATH];
  int fd, omode;
  struct file *f;
  struct inode *ip;
  int n;

  argint(1, &omode);
  if((n = argstr(0, path, MAXPATH)) < 0)
    return -1;

  begin_op();

  if(omode & O_CREATE){
    ip = create(path, T_FILE, 0, 0);
    if(ip == 0){
      end_op();
      return -1;
    }
  } else {
    if((ip = namei(path)) == 0){
      end_op();
      return -1;
    }
    ilock(ip);
    if(ip->type == T_SYMLINK&&!(omode&O_NOFOLLOW)){
      if((ip=follow_symlink(ip, 0))==0){
        end_op();
        return -1;
      }
    }
    if(ip->type == T_DIR && omode != O_RDONLY){
      iunlockput(ip);
      end_op();
      return -1;
    }
  }

  if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
    iunlockput(ip);
    end_op();
    return -1;
  }

  if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
    if(f)
      fileclose(f);
    iunlockput(ip);
    end_op();
    return -1;
  }

  if(ip->type == T_DEVICE){
    f->type = FD_DEVICE;
    f->major = ip->major;
  } else {
    f->type = FD_INODE;
    f->off = 0;
  }
  f->ip = ip;
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);

  if((omode & O_TRUNC) && ip->type == T_FILE){
    itrunc(ip);
  }

  iunlock(ip);
  end_op();

  return fd;
}

//0表示成功 -1表示失败
uint64
sys_symlink(void)
{
  char target[MAXPATH], path[MAXPATH];
  struct inode *ip;

  if(argstr(0, target, MAXPATH) < 0 || argstr(1, path, MAXPATH) < 0)
    return -1;
  begin_op();
  
  //创建符号链接文件
  if((ip = create(path, T_SYMLINK, 0, 0)) == 0){
    end_op();
    return -1;
  }
  // 将target路径存储在inode的数据块中
  if(writei(ip, 0, (uint64)target, 0, strlen(target)) != strlen(target)){
    iunlockput(ip);
    end_op();
    return -1;
  }
  
  
  iunlockput(ip);
  end_op();
  return 0;
}
MIT6.828 Lab 5: File system, Spawn and Shell 文件系统 阅读详情

相关推荐

AB相增量式编码器以及STM32定时器编码器模式测速/测距离(HAL库)

编码器(Encoder)是一种用于测量旋转运动或位置的设备。编码器通常与定时器模块一起使用,以便在微控制器中获取和计算旋转的脉冲数量,从而确定物体的运动方向和距离。编码器在许多应用中都很有用,例如机器人运动控制、电机位置反馈和位置传感。STM32微控制器提供了多种类型的编码器支持,其中一种常见的编码器类型是增量编码器。增量编码器基于两个脉冲信号通道,通常称为A相和B相。当旋转发生时,这两个信号通道的相位关系会发生变化,从而可以计算出旋转方向和旋转的步数。

嵌入式软件工程师的博客 1万+

MIT6.S081 Lab7: file system

这节实验的难度相对来说不高,9.1主要是要理解原代码是如何实现申请直接块和一级间接块,9.2需要先理解文件系统、符号链接的一些概念,然后实现起来也不难。

weixin_43724444的博客 795

局域网ip冲突检测工具_python 小工具实现 windows笔记本与 ipad数据互传

宅在家没事的时候就开始琢磨ipad的进阶使用,由于没有mac无法隔空投送,ipad与windows的数据交互表现极差,所以设计了这个ftp小工具。当然,这个工具不仅仅能用于windows与ipad,还可以用于局域网任意两台设备之间程序源码与下载见文章末尾使用说明windows端:biubiubiu.exeipad端: appsotre下载 FE文件管理器windows端操作在c盘下创建ftp目录...

weixin_39866265的博客 4440

6.s081 Lab:file system

Lab:file system 1.实验目的 向xv6文件系统添加大文件和符号链接。 2.实验内容 1)在这个任务中,我们需要增加xv6文件的最大大小。目前xv6文件被限制为268个块,也就是268BSIZE字节(在xv6中BSIZE是1024)。这个限制来自于这样一个限定:一个xv6索引节点包含12个直接索引和一个一级间接索引(最多可容纳256个块号的块),总共有12+256=268个块。我们需要更改xv6文件系统代码,以支持每个inode中的二级间接索引,其中包含256个一级间接索引块的地址,每个块最多

pige666的博客 4200

基于FMCW技术的毫米波雷达测速原理与应用

调频连续波(FMCW)技术是毫米波雷达中实现高精度测速的核心方法之一。本文详细介绍了FMCW测速的基本原理、信号处理流程以及在实际应用中的关键技术。通过分析FMCW雷达的多普勒效应和频率差测量方法,本文展示了如何利用FMCW技术实现目标速度的高精度检测。此外,本文还探讨了FMCW测速在自动驾驶、智能交通和工业自动化等领域的应用案例,为毫米波雷达开发者提供了理论支持和实践指导。

不谈枯燥公式,只聊雷达趋势。用大白话解读毫米波里的黑科技。 1146

Kubernetes集群资源优化实战:Volcano调度器如何帮你省下30%GPU算力

本文详细介绍了如何通过Volcano调度器的Binpack策略优化Kubernetes集群中的GPU资源利用率,解决AI模型训练中的资源碎片化问题。通过实战配置和调优指南,帮助用户提升30%的GPU算力利用率,显著降低任务等待时间和硬件成本。

weixin_30832143的博客 512

xv6 Lab: file system

一定要仔细阅读xv6book的file system部分 阅读关于file的代码。比如创建新的inode的create()调用后会持有返回inode的锁。这一部分我认为是比较难的 如果要写的比较严谨 要考虑的地方还是很多的。主要的细节在最初的bmap()中已经体现到了 照葫芦画瓢即可。要修改fs.h的结构体 fs.c中的bmap()函数。否则去测试的时候跟没修改过的bmap()是一样的。要注意块号bn与连续的块的索引到底是什么关系。但在测试的时候发现给的用例可能没有这么严谨。

Holddom的博客 537

FortiGate-60E防火墙忘记密码?用Console线+TFTP重装固件v7.0.9全流程

本文详细介绍了当FortiGate-60E防火墙忘记密码且物理重置无效时,如何通过Console线连接并利用TFTP服务器重装固件(如v7.0.9)的全流程。从恢复原理、工具准备、网络配置到逐步执行命令,提供了完整的实战指南,帮助管理员彻底恢复设备出厂设置并重置密码。

snow3的博客 342

9. Lab: file system

https://pdos.csail.mit.edu/6.S081/2021/labs/fs.html1. Large files (moderate)1.1 要求Modify bmap() so that it implements a doubly-indirect block, in addition to direct blocks and a singly-indirect block. You'll have to have only 11 direct blocks, rather than

cmdos的专栏 562

数字孪生驱动的城市地下管网智能运维与应急处置决策支持系统

上海市城市数字化中心的数据实践显示,通过关联分析管网材质与故障率的关系,将铸铁管更换周期从15年优化至8年(李敏,2021)。环境效益方面,上海市通过优化管网运行参数,年减少碳排放1.8万吨,相当于种植36万棵乔木。数字孪生与物理设备的双向交互采用OPC UA协议实现,某次天津管网渗漏事件中,系统通过实时数据反馈,指导现场人员调整注浆方案,将修复时间从48小时压缩至16小时。虚拟指挥沙盘支持多视角协同,深圳水务系统的实践表明,通过AR可视化技术,跨部门会议效率提升60%,方案决策时间缩短40%。

2501_92431686的博客 1431

【GameFramework框架】7-2、GameFramework框架是否“过度设计”?

框架是否过度设计,这个问题其实我被问到很多次了。不仅仅是新手刚接触就觉得“过度设计”的评价,有些熟练的开发者也会有这种感觉。那么,这篇文章就来探究一下,为什么框架会让人感觉“过度设计”,以及框架如何设计,还有,如何让框架适应自己的节奏。

CSDN博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 2131

COMP20005 Engineering Computation Semester 2, 2022

COMP20005 Engineering Computation Semester 2, 2022

Faker_1024的博客 841

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Lab:file system

lab:file system

maoyl0506的博客 1154

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Lab9: file system

这几天做Lab的速度快了很多,因为我没看课程,直接看的课本,发现自己真的不适合看视频,看着看着就要睡着了,莫里斯教授的还好,另一个教授讲的真的让我昏昏欲睡,每个人都有自己的学习方法,大家也不用盲信网上讲的各种学习方法,适合自己的最重要。

m0_61815263的博客 460

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【xv6】Lab: file system

【代码】【xv6】Lab: file system。

m0_48446356的博客 494

MIT 6s081 lab9:file system

文件系统的实验,需要对提前阅读fs.c\bio.c\sysfile.c以及相关头文件。

厚积而薄发 1442

[MIT 6.S081] Lab 9: file system

Lab 9: file system Lab Guidance: https://pdos.csail.mit.edu/6.828/2020/labs/fs.html Lab Code: https://github.com/VastRock-Huang/xv6-labs-2020/tree/fs Large files (moderate) 要点 实现二级间接块索引(doubly-indirect block)的 inode 结构: ip->addrs[] 的前 11 个元素是直接块(dire

LostUnravel的博客 7930

MIT Operating Systems Lab: file system

Lab: file systemLarge filescode2.读入数据总结 Large files your job: Modify bmap() so that it implements a doubly-indirect block, in addition to direct blocks and a singly-indirect block. You’ll have to have only 11 direct blocks, rather than 12, to make room fo

sutungpo的博客 670

6.S081的Lab学习——Lab9: file system

一个本硕双非的小菜鸡,备战24年秋招。打算尝试6.S081,将它的Lab逐一实现,并记录期间心酸历程。

qq_43264167的博客 1240

Lab 5: File system, Spawn and Shell

从头开始,read(我们提供的)工作于任何文件描述符,并简单地分派给适当的设备read函数,在本例中是devfile_read(我们可以有更多的设备类型,如管道)。因此,对于任何大于40KB的文件,我们分配了一个额外的磁盘块,称为文件的间接块,用于存储最多4096/4 = 1024个额外的块号。要从客户端向服务器发送请求,我们使用32位数字作为请求类型(文件系统服务器的rpc是编号的,就像系统调用编号一样),并将请求的参数存储在通过IPC共享的页面上的union Fsipc中。)是打印到fd1的快捷方式。

weixin_45459266的博客 315

MIT 6.S081 Lab9: file system

这个实验是文件系统,内容是扩充 xv6 的文件系统大小和实现软链接,实验地址在这里。感觉到这里是操作系统的另一个视图了,跟前面的虚拟内存和进程不太一样,相比来说,操作系统这部分的知识简单一些。为此我去补充了一下相关的知识,再次感觉这门课真好,。能力太差了,写代码花了好长时间,,代码放在了github上。

落队的飞行兵 2217

2020 6.s081——Lab9:File system

在跟踪符号链接时需要额外考虑到符号链接的目标可能还是符号链接, 此时需要递归的去跟踪目标链接直至得到真正的文件。

John_Snowww的博客 1039

lab 9:Fs(file system)

通读第八章以及观看教学视频,理解目录与文件的组织方式,思考与阅读源码的实现方式。总的来说,本次作业难度不大,主要是对文件系统抽象的理解。

cube__4的博客 524

笔记 Lab9: File System | 文件系统

Large files (moderate) Modify bmap() so that it implements a doubly-indirect block, in addition to direct blocks and a singly-indirect block. You’ll have to have only 11 direct blocks, rather than 12, to make room for your new doubly-indirect block; you’r

陌养的编程之路 830
上一篇: Lab: networking
Peiris_
博客等级 码龄7年 44粉丝 8原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值