iT邦幫忙

0

[6.1810][code] xv6 的 FileSystem (一) : 概述

  • 分享至 

  • xImage
  •  

系列文章 : [6.1810] 跟著 MIT 6.1810 學習基礎作業系統觀念

檔案系統的目的 :

  • 檔案系統的目的是組織和儲存資料。
  • 檔案系統通常具備持久性,也就是在重新開機後,資料仍舊存在。

在 xv6-riscv 上的檔案系統解決了幾個麻煩 :

  • 檔案系統需要 disk 上的 data structure 來表示
    • tree of named directories and files
    • 記錄 identities of the blocks that hold each file’s content
    • 記錄 disk 上哪些區域是空閒的
  • 檔案系統必須支援 crash recovery。假如發生 crash ( 例如說突然被關機 ),檔案系統必須要在重新開機後仍然正確運作。因為當突發性的 crash 發生的時候,可能會中斷檔案系統的操作,導致 on-disk data structure inconsistent ( 一個 block 同時被標記為 free,又同時被一個檔案使用 )。
  • 不同的 process 可能會想要同時操作同一個檔案系統 ( file system ),所以檔案系統的程式碼必須要能夠協調不同 process 對同一個檔案系統的操作。
  • 存取 disk 的速度比存取 memory 慢上好幾個數量級 ( orders of magnitude ),因此檔案系統必須維護 in-memory cache of popular blocks


xv6-riscv 的 file-system 被分成 7 層,這邊由最頂層的 Layer 往下列舉。

  • File Descriptor Layer
    • 相關檔案
      • file.c
      • sysfile.c
    • 簡介 : File Descriptor Layer 抽象化了許多 Unix 資源 ( e.g. pipes, devices, files, etc… ),使用檔案系統提供的介面,簡化了應用程式開發人員的工作。
  • Pathname Layer
    • 相關檔案
      • fs.c
    • 簡介 : pathname layer 提供了如 /usr/rtm/xv6/fs.c 般的階層式路徑名稱,並透過遞迴查找 ( recursive lookup ) 來解析 ( resolve ) 它們
  • Directory Layer
    • 相關檔案
      • fs.c
    • 簡介 : directory layer 把每一個目錄 ( directory ) 實作成一個特殊的 inode,這個特殊 inode 的內容為一連串的目錄條目,每個條目包含一個檔案名稱與 i-number。
  • Inode Layer
    • 相關檔案
      • fs.c
      • fs.h
    • 簡介 : inode layer 提供獨立的檔案,每個檔案皆表示為一個具有唯一 i-number 的 inode,以及一些存放該檔案資料的 blocks
    • 註 : block 是作業系統 ( 軟體 ) 的概念,而 sector 則是硬體 ( e.g. disk ) 的概念,通常 block size 是 sector size 的倍數。例如 xv6-riscv 的 block size 是 1024 bytes,而 VirtIO block device 的 sector size 是 512 bytes。1024 為 512 的倍數。
  • Logging Layer
    • 相關檔案
      • log.c
    • 簡介 : logging layer 允許更高層級 ( e.g. inode-layer … ) 對數個 blocks 的更新包裝在一個 transaction 中,並確保在面對 crash ( e.g. 突然關機 ) 的時候,對這些 blocks 的更新具有原子性 ( atomic, 全部更新或是全部都不更新 )。
  • Buffer Cache Layer
    • 相關檔案
      • bio.c
      • buf.h
    • 簡介 : Buffer Cache Layer 會對 disk blocks 進行快取,並且確定每次只有一個 kernel process 可以對一個 block 進行操作。
  • Disk Layer
    • 相關檔案
      • virtio.h
      • virtio_disk.c
    • 簡介 : 針對 VirtIO Block Device 的操作,對 VritIO Block Device 的 sector 進行讀寫。


  • disk hardware 傳統上會將 disk 上的資料呈現為一系列擁有編號的 512-byte blocks ( 也能被稱為 sectors )。
  • sector 0 為前 512 bytes,sector 1 是接下來的 512 bytes,以此類推。
  • 作業系統中使用的 block size 不一定會跟 disk hardware 使用的 block size ( or sector size ) 同,但通常作業系統所使用的 block size 會是 disk hardware 的 sector size 的倍數。例如 xv6-riscv 的 block size 是 1024 bytes,而 VirtIO block device 的 sector size 是 512 bytes。1024 為 512 的倍數。
  • xv6-riscv 會把讀進 memory ( e.g. DRAM ) 的 blocks 的副本,保存在 struct buf 中。
  • 儲存在 struct buf 中的資料,有時候會跟 disk hardware 不同步。
    • struct buf 中的資料可能尚未從 disk 讀入 ( disk 正在處理,但尚未回傳 sectors 的內容 )
    • struct buf 中的資料可能已經被軟體更新,但是還沒有寫進磁碟。


  • 檔案系統必須規劃 inode 和內容區塊 ( content blocks ) 儲存在 disk 上的位置。
  • xv6-riscv 把 disk 劃分為幾個不同的區段 ( sections ),可以參考下面的圖例。
  • 檔案系統不會去使用 block 0 ( 因為 block 0 已經被當作 boot sector 使用 )。
  • Block 1 被稱為 superblock
    • 它包含了 metadata
      • 以 block 為單位的,檔案系統的大小
      • the number of data blocks
      • the number of inodes
      • the number of blocks in the log
  • 從 Block 2 開始存放日誌 ( log )
  • 在日誌 ( log ) 後面是 inode,每個 block 會包含多個 inode。
  • 在這之後是 bitmap block,用於追蹤哪些 block 正在被使用。
  • 剩餘的 blocks 就是 data blocks,每一個 data blocks 要麼是被標記為空閒 ( free ),要麼就是存放著 檔案的內容( content for a file ),或是目錄 ( directory ) 的內容。
  • superblock 由一個名為 mkfs獨立程式 (seperate program) 填入。mkfs 這個程式負責建立初始的檔案系統。

{ 來自 xv6-riscv 課本的圖例 }
https://ithelp.ithome.com.tw/upload/images/20260503/20180992TDC7g3YQba.png



在 xv6-riscv,file system 在軟體上被分為七個 layer ( disk, buffer cache, logging, inode, directory, pathname, file descriptor ),以及在 disk hardware 內的 layout ( boot, superblock, log, inodes, bitmap, data ),這是 xv6-riscv 對檔案系統的設計。

其他的檔案系統 (e.g. ext4, FAT) 可能會有不同的設計 ( 軟體的分層設計,以及 disk hardware 內的 layout )。




圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言