系列文章 : [6.1810] 跟著 MIT 6.1810 學習基礎作業系統觀念
檔案系統的目的 :
在 xv6-riscv 上的檔案系統解決了幾個麻煩 :
identities of the blocks that hold each file’s content
crash recovery。假如發生 crash ( 例如說突然被關機 ),檔案系統必須要在重新開機後仍然正確運作。因為當突發性的 crash 發生的時候,可能會中斷檔案系統的操作,導致 on-disk data structure inconsistent ( 一個 block 同時被標記為 free,又同時被一個檔案使用 )。in-memory cache of popular blocks。xv6-riscv 的 file-system 被分成 7 層,這邊由最頂層的 Layer 往下列舉。
File Descriptor Layer 抽象化了許多 Unix 資源 ( e.g. pipes, devices, files, etc… ),使用檔案系統提供的介面,簡化了應用程式開發人員的工作。pathname layer 提供了如 /usr/rtm/xv6/fs.c 般的階層式路徑名稱,並透過遞迴查找 ( recursive lookup ) 來解析 ( resolve ) 它們directory layer 把每一個目錄 ( directory ) 實作成一個特殊的 inode,這個特殊 inode 的內容為一連串的目錄條目,每個條目包含一個檔案名稱與 i-number。inode layer 提供獨立的檔案,每個檔案皆表示為一個具有唯一 i-number 的 inode,以及一些存放該檔案資料的 blocks。logging layer 允許更高層級 ( e.g. inode-layer … ) 對數個 blocks 的更新包裝在一個 transaction 中,並確保在面對 crash ( e.g. 突然關機 ) 的時候,對這些 blocks 的更新具有原子性 ( atomic, 全部更新或是全部都不更新 )。Buffer Cache Layer 會對 disk blocks 進行快取,並且確定每次只有一個 kernel process 可以對一個 block 進行操作。sector 進行讀寫。擁有編號的 512-byte blocks ( 也能被稱為 sectors )。struct buf 中。struct buf 中的資料,有時候會跟 disk hardware 不同步。
struct buf 中的資料可能尚未從 disk 讀入 ( disk 正在處理,但尚未回傳 sectors 的內容 )struct buf 中的資料可能已經被軟體更新,但是還沒有寫進磁碟。superblock。
metadata
bitmap block,用於追蹤哪些 block 正在被使用。data blocks,每一個 data blocks 要麼是被標記為空閒 ( free ),要麼就是存放著 檔案的內容( content for a file ),或是目錄 ( directory ) 的內容。superblock 由一個名為 mkfs 的獨立程式 (seperate program) 填入。mkfs 這個程式負責建立初始的檔案系統。{ 來自 xv6-riscv 課本的圖例 }
在 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 )。