我在學習研究Linux內核結構的時候,思考過一個問題:Linux如何定位文件在磁盤的物理位置
每個文件都有一個inode,inode在內核代碼中的數據結構如下:
1 struct ext4_inode { 2 __le16 i_mode; /* File mode */ 3 __le16 i_uid; /* Low 16 bits of Owner Uid */ 4 __le32 i_size_lo; /* Size in bytes */ 5 __le32 i_atime; /* Access time */ 6 __le32 i_ctime; /* Inode Change time */ 7 __le32 i_mtime; /* Modification time */ 8 __le32 i_dtime; /* Deletion Time */ 9 __le16 i_gid; /* Low 16 bits of Group Id */ 10 __le16 i_links_count; /* Links count */ 11 __le32 i_blocks_lo; /* Blocks count */ 12 __le32 i_flags; /* File flags */ 13 ...... 14 __le32 i_block[EXT4_N_BLOCKS];/* Pointers to blocks */ 15 __le32 i_generation; /* File version (for NFS) */ 16 __le32 i_file_acl_lo; /* File ACL */ 17 __le32 i_size_high; 18 ...... 19 };
我們說的“某個文件分成幾塊、每一塊在哪里”,這些在 inode 里面,應該保存在 i_block( i_block[EXT4_N_BLOCKS];/* Pointers to blocks */)里面
為了解決ext2和ext3文件系統大文件讀寫性能低下的問題,ext4 做了一定的改變。它引入了一個新的概念,叫作 Extents。我們來解釋一下 Extents。比方說,一個文件大小為 128M,如果使用 4k 大小的塊進行存儲,需要 32k 個塊。如果按照 ext2 或者 ext3 那樣散着放,數量太大了。但是 Extents 可以用於存放連續的塊,也就是說,我們可以把 128M 放在一個 Extents 里面。這樣的話,對大文件的讀寫性能提高了,文件碎片也減少了。
Exents 如何來存儲呢?它其實會保存成一棵樹。

樹有一個個的節點,有葉子節點,也有分支節點。每個節點都有一個頭,ext4_extent_header 可以用來描述某個節點
struct ext4_extent_header { __le16 eh_magic; /* probably will support different formats */ __le16 eh_entries; /* number of valid entries */ __le16 eh_max; /* capacity of store in entries */ __le16 eh_depth; /* has tree real underlying blocks? */ __le32 eh_generation; /* generation of the tree */ };
我們仔細來看里面的內容。eh_entries 表示這個節點里面有多少項。這里的項分兩種,如果是葉子節點,這一項會直接指向硬盤上的連續塊的地址,我們稱為數據節點 ext4_extent;如果是分支節點,這一項會指向下一層的分支節點或者葉子節點,我們稱為索引節點 ext4_extent_idx。這兩種類型的項的大小都是 12 個 byte。
/* * This is the extent on-disk structure. * It's used at the bottom of the tree. */ struct ext4_extent { __le32 ee_block; /* first logical block extent covers */ __le16 ee_len; /* number of blocks covered by extent */ __le16 ee_start_hi; /* high 16 bits of physical block */ __le32 ee_start_lo; /* low 32 bits of physical block */ }; /* * This is index on-disk structure. * It's used at all the levels except the bottom. */ struct ext4_extent_idx { __le32 ei_block; /* index covers logical blocks from 'block' */ __le32 ei_leaf_lo; /* pointer to the physical block of the next * * level. leaf or next index could be there */ __le16 ei_leaf_hi; /* high 16 bits of physical block */ __u16 ei_unused; };
從上述數據結構中的ee_start_hi/ee_start_lo/ei_leaf_lo可以體現出物理塊的指針。
參考文獻:《趣談Linux操作系統》 by 劉超
參考鏈接:https://blog.csdn.net/stringnewname/article/details/73740155