xv6Inode - ccc-sp/riscv2os GitHub Wiki
xv6: inode 是甚麼?如何用來儲存檔案?
inode 是 UNIX 中用來代表檔案的一種資料結構,如下圖所示:

在 xv6 中,inode 宣告於 file.h 中
kernel/file.h
// in-memory copy of an inode (記憶體內的 inode)
struct inode {
uint dev; // Device number (裝置號碼)
uint inum; // Inode number (inode 號碼)
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 (inode 型態)
short major; // 主序號
short minor; // 次序號
short nlink; // 連結次數
uint size; // 檔案大小
uint addrs[NDIRECT+1]; // 區塊位址
};
其中的 addrs[i] 是資料區塊的代號,前面 NDIRECT 個都是直接區塊 (儲存檔案內容),而最後一個是間接區塊 (指向另一個 inode) 。
但以上的 inode 是記憶體中的結構,其中很多欄位不需要儲存在硬碟裏,另外有個 dinode 的結構,才是真正儲存在硬碟的樣子。
// On-disk inode structure (硬碟中的 inode 結構)
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+1]; // Data block addresses
};