Linux如何定位文件在磁盤的物理位置

我在學習研究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 能夠用來描述某個節點node

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/73740155ui

相關文章
相關標籤/搜索