深入理解Linux文件系統(tǒng)之Ext2路徑名查找
本文轉(zhuǎn)載自微信公眾號(hào)「Linux內(nèi)核遠(yuǎn)航者」,作者Linux內(nèi)核遠(yuǎn)航者。轉(zhuǎn)載本文請(qǐng)聯(lián)系Linux內(nèi)核遠(yuǎn)航者公眾號(hào)。
本文以ext2文件系統(tǒng)為例來剖析一個(gè)真實(shí)的文件系統(tǒng)如何查找文件,這對(duì)于深入理解文件系統(tǒng)至關(guān)重要。
1.準(zhǔn)備文件系統(tǒng)鏡像
所用工具:dd、mkfs.ext2、hexdump、dumpe2fs、mount等工具
1)制作100k大小鏡像文件
- $ dd if=/dev/zero of=ext2.img bs=1k count=100
- 記錄了100+0 的讀入
- 記錄了100+0 的寫出
- 102400 bytes (102 kB, 100 KiB) copied, 0.00125457 s, 81.6 MB/s
2)格式化為ext2文件系統(tǒng)格式
- $ mkfs.ext2 ext2.img
- mke2fs 1.44.1 (24-Mar-2018)
- 丟棄設(shè)備塊: 完成
- 創(chuàng)建含有 100 個(gè)塊(每塊 1k)和 16 個(gè)inode的文件系統(tǒng)
- 正在分配組表: 完成
- 正在寫入inode表: 完成
- 寫入超級(jí)塊和文件系統(tǒng)賬戶統(tǒng)計(jì)信息: 已完成
3)查看文件系統(tǒng)信息
- $ dumpe2fs ext2.img
- dumpe2fs 1.44.1 (24-Mar-2018)
- Filesystem volume name: <none>
- Last mounted on: <not available>
- Filesystem UUID: 3680e1d5-7f58-4324-9cbd-c7d382f0c3df
- Filesystem magic number: 0xEF53
- Filesystem revision #: 1 (dynamic)
- Filesystem features: ext_attr resize_inode dir_index filetype sparse_super large_file
- Filesystem flags: signed_directory_hash
- Default mount options: user_xattr acl
- Filesystem state: clean
- Errors behavior: Continue
- Filesystem OS type: Linux
- Inode count: 16
- Block count: 100
- Reserved block count: 5
- Free blocks: 79
- Free inodes: 5
- First block: 1
- Block size: 1024
- Fragment size: 1024
- Blocks per group: 8192
- Fragments per group: 8192
- Inodes per group: 16
- Inode blocks per group: 2
- Filesystem created: Wed May 26 15:23:33 2021
- Last mount time: n/a
- Last write time: Wed May 26 15:23:33 2021
- Mount count: 0
- Maximum mount count: -1
- Last checked: Wed May 26 15:23:33 2021
- Check interval: 0 (<none>)
- Reserved blocks uid: 0 (user root)
- Reserved blocks gid: 0 (group root)
- First inode: 11
- Inode size: 128
- Default directory hash: half_md4
- Directory Hash Seed: 5b0daa29-c2a0-4ab1-b09e-50992d3b070d
- 組 0:(塊 1-99)
- 主 超級(jí)塊位于 1,組描述符位于 2-2
- 塊位圖位于 3 (+2)
- Inode 位圖位于 4 (+3)
- Inode表位于 5-6 (+4)
- 79 個(gè)可用 塊,5 個(gè)可用inode,2 個(gè)目錄
- 可用塊數(shù): 21-99
- 可用inode數(shù): 12-16
這實(shí)際是是讀取文件系統(tǒng)的超級(jí)塊和塊組描述符信息。我們可以看的創(chuàng)建的文件系統(tǒng)的總體信息:
Filesystem magic number:0xEF53 表示為ext2文件系統(tǒng)
Inode count: 16 表示文件系統(tǒng)inode個(gè)數(shù)為16
Block count: 100 表示文件系統(tǒng)塊個(gè)數(shù)為100
Free blocks: 79 表示文件系統(tǒng)空閑塊個(gè)數(shù)為79
Free inodes: 5 表示文件系統(tǒng)空閑inode個(gè)數(shù)為5
First block: 1 第一個(gè)數(shù)據(jù)塊編號(hào)為1(編號(hào)0保留為引導(dǎo)塊)
Block size: 1024 文件系統(tǒng)塊大小為1k
Blocks per group: 8192 每個(gè)塊組8192個(gè)塊
Inodes per group: 16 每個(gè)塊組個(gè)inode
Inode blocks per group: 2 每個(gè)塊組2個(gè)inode塊
First inode: 11 分配的第一個(gè)inode號(hào)為11(除根inode外,根inode號(hào)為2)
Inode size: 128 inode大小為128字節(jié)
塊組的信息(這里只有一個(gè)塊組) 1 - 99號(hào)
超級(jí)塊塊編號(hào)為 1
塊組描述符塊編號(hào)為 2 塊
位圖塊編號(hào)為 3
inode位圖塊編號(hào)為 4
inode表位于5和6塊
79 個(gè)可用 塊,5 個(gè)可用inode,2 個(gè)目錄 (一個(gè)為根目錄一個(gè)為lost+found,存放壞塊) 可用塊數(shù):21-99 可用inode數(shù):12-16
4)掛載文件系統(tǒng)并創(chuàng)建文件
- 創(chuàng)建一個(gè)掛載點(diǎn)目錄:
- $ mkdir root_dir
- 掛載:
- $ sudo mount -t ext2 ext2.img root_dir
- 查看文件:
- $ ls -la
- 總用量 17
- drwxr-xr-x 3 root root 1024 5月 26 15:23 .
- drwxrwxr-x 3 hanch hanch 4096 5月 26 15:28 ..
- drwx------ 2 root root 12288 5月 26 15:23 lost+found
可以發(fā)現(xiàn)有三個(gè)目錄:
- .
- ..
- lost+found
實(shí)際上是根目錄的數(shù)據(jù)塊的內(nèi)容(包含各個(gè)目錄項(xiàng))。
下面我們來創(chuàng)建一個(gè)目錄,目錄下創(chuàng)建文件:
- $ sudo mkdir dir
- $ cd dir/
- $ su
- # echo hello > test.txt
現(xiàn)在目錄樹是這樣的:
- $ tree
- .
- ├── dir
- │ └── test.txt
- └── lost+found [error opening dir]
- 2 directories, 1 file
后面我們會(huì)通過解析文件系統(tǒng)鏡像來觀察如何查找 /dir/test.txt 文件的
現(xiàn)在關(guān)注一下相關(guān)的索引節(jié)點(diǎn):
- $ cd dir
- $ ls -lai
- 總用量 3
- 12 drwxr-xr-x 2 root root 1024 5月 26 15:57 .
- 2 drwxr-xr-x 4 root root 1024 5月 26 15:56 ..
- 13 -rw-r--r-- 1 root root 6 5月 26 15:57 test.txt
可以發(fā)現(xiàn) /dir目錄下:當(dāng)前工作目錄下索引節(jié)點(diǎn)為12(dir目錄的),上一級(jí)目錄的索引節(jié)點(diǎn)為2(根目錄),test.txt文件的所有節(jié)點(diǎn)為13。記住這幾個(gè)索引節(jié)點(diǎn)后面我們會(huì)通過解析文件系統(tǒng)鏡像來獲得。
2.解析文件系統(tǒng)鏡像
1)dump文件系統(tǒng)鏡像
- $ hexdump -C ext2.img
- 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
- 00000400 10 00 00 00 64 00 00 00 05 00 00 00 4f 00 00 00 |....d.......O...|
- 00000410 05 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 |................|
- 00000420 00 20 00 00 00 20 00 00 10 00 00 00 ae f8 ad 60 |. ... .........`|
- 00000430 ae f8 ad 60 01 00 ff ff 53 ef 00 00 01 00 00 00 |...`....S.......|
- 00000440 75 f7 ad 60 00 00 00 00 00 00 00 00 01 00 00 00 |u..`............|
- ...
- *
- 00018c00 0c 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00 |................|
- 00018c10 0c 00 02 02 2e 2e 00 00 0d 00 00 00 e8 03 08 01 |................|
- 00018c20 74 65 73 74 2e 74 78 74 00 00 00 00 00 00 00 00 |test.txt........|
- 00018c30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
- 00019000
根據(jù)之前dumpe2fs的信息我們知道:
鏡像文件中(均為16進(jìn)制顯示)
00000000 開始的1k大小 保留的引導(dǎo)塊
塊1 00000400 開始的1k大小 保存磁盤的超級(jí)塊 (dumpe2fs的部分信息從這里獲得)
塊2 00000800 開始的1k大小 保存塊組描述符 (dumpe2fs的部分信息從這里獲得)
塊3 00000c00 開始的1k大小 保存塊位圖
塊4 00001000 開始的1k大小 保存 Inode 位圖
塊5 塊6 00001400 開始的2k大小 保存 Inode表
剩下的為數(shù)據(jù)塊
磁盤中的文件系統(tǒng)對(duì)象結(jié)構(gòu)在內(nèi)核如下文件定義:
- fs/ext2/ext2.h
- 磁盤超級(jí)塊:
- struct ext2_super_block {
- __le32 s_inodes_count; /* Inodes count */
- __le32 s_blocks_count; /* Blocks count */
- __le32 s_r_blocks_count; /* Reserved blocks count */
- __le32 s_free_blocks_count; /* Free blocks count */
- __le32 s_free_inodes_count; /* Free inodes count */
- __le32 s_first_data_block; /* First Data Block */
- __le32 s_log_block_size; /* Block size */
- __le32 s_log_frag_size; /* Fragment size */
- __le32 s_blocks_per_group; /* # Blocks per group */
- __le32 s_frags_per_group; /* # Fragments per group */
- __le32 s_inodes_per_group; /* # Inodes per group */
- ...
- }
- 磁盤塊組描述符:
- struct ext2_group_desc
- {
- __le32 bg_block_bitmap; /* Blocks bitmap block */
- __le32 bg_inode_bitmap; /* Inodes bitmap block */
- __le32 bg_inode_table; /* Inodes table block */
- __le16 bg_free_blocks_count; /* Free blocks count */
- __le16 bg_free_inodes_count; /* Free inodes count */
- __le16 bg_used_dirs_count; /* Directories count */
- __le16 bg_pad;
- __le32 bg_reserved[3];
- };
- 磁盤inode:
- struct ext2_inode {
- __le16 i_mode; /* File mode */
- __le16 i_uid; /* Low 16 bits of Owner Uid */
- __le32 i_size; /* Size in bytes */
- __le32 i_atime; /* Access time */
- __le32 i_ctime; /* Creation time */
- __le32 i_mtime; /* Modification time */
- __le32 i_dtime; /* Deletion Time */
- __le16 i_gid; /* Low 16 bits of Group Id */
- __le16 i_links_count; /* Links count */
- __le32 i_blocks; /* Blocks count */
- ...
- __le32 i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
- ...
- };
- 磁盤目錄項(xiàng):
- struct ext2_dir_entry_2 {
- __le32 inode; /* Inode number */
- __le16 rec_len; /* Directory entry length */
- __u8 name_len; /* Name length */
- __u8 file_type;
- char name[]; /* File name, up to EXT2_NAME_LEN */
- };
大家可以對(duì)照磁盤鏡像文件和磁盤數(shù)據(jù)結(jié)構(gòu)定義來解析出文件系統(tǒng)的超級(jí)塊和塊組描述符信息(可以發(fā)現(xiàn)和dumpe2fs工具顯示的是一致的,例如鏡像文件00000400 處四字節(jié)為10 00 00 00 是小端存儲(chǔ),所以為0x00000010=16);
3.路徑名查找
下面開始我們的重頭戲:查找文件系統(tǒng)中的 /dir/test.txt 文件。
我們知道,使用文件系統(tǒng)給我最直觀也是最大的好處是:用戶可以通過一個(gè)路徑名來訪問文件,那么一個(gè)文件系統(tǒng)究竟如何來找到我們所需要的文件呢?下面我們?cè)敿?xì)來看ext2文件系統(tǒng)如何查找指定的文件的?(實(shí)際的內(nèi)核中路徑名查找比較復(fù)雜,考慮很多情況,如dentry cache查找、解析軟鏈接文件、上級(jí)目錄、掛載點(diǎn)等,當(dāng)然如果目錄分量是掛載點(diǎn)就會(huì)步進(jìn)到相應(yīng)文件系統(tǒng)的根目錄,后面文件系統(tǒng)掛載專題會(huì)講解,這里以簡(jiǎn)單的路徑解析來讓大家有個(gè)深刻的認(rèn)識(shí))。
1)查找根目錄
萬事開頭難,對(duì)于訪問一個(gè)目錄上掛載的文件系統(tǒng),內(nèi)核路徑名查找會(huì)判斷并找到掛載的文件系統(tǒng)的根目錄,這個(gè)過程在文件系統(tǒng)掛載的時(shí)候,會(huì)從磁盤上讀取并在內(nèi)存構(gòu)建超級(jí)塊實(shí)例,然后進(jìn)行的最重要的一步是讀取文件系統(tǒng)的根inode:
- fs/ext2/super.c
- ext2_fill_super
- ->root = ext2_iget(sb, EXT2_ROOT_INO) //EXT2_ROOT_INO為2,系統(tǒng)定義好的
- ->raw_inode = ext2_get_inode(inode->i_sb, ino, &bh); //根據(jù)inode號(hào)查找磁盤inode
- 核心算法如下:
- ->block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb) //獲得塊組 編號(hào)
- gdp = ext2_get_group_desc(sb, block_group, NULL); //獲得塊組描述符
- offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb); //計(jì)算出在 塊組的 inode表中的inode偏移
- block = le32_to_cpu(gdp->bg_inode_table) + (offset >> EXT2_BLOCK_SIZE_BITS(sb)); //計(jì)算出在文件系統(tǒng)中的塊號(hào)
- bh = sb_bread(sb, block)) //組合成submit_bh 讀取這個(gè)塊到bh
- *p = bh; //賦值bh 用于返回
- offset &= (EXT2_BLOCK_SIZE(sb) - 1); //計(jì)算出塊中偏移
- return (struct ext2_inode *) (bh->b_data + offset); //返回inode中位置
簡(jiǎn)述ext2通過inode號(hào)找到并讀取磁盤inode核心算法:
1.根據(jù)inode號(hào)計(jì)算出所在的塊組block_group
2.根據(jù)inode號(hào)計(jì)算出塊組中的inode表中的字節(jié)偏移offset
3.根據(jù)inode號(hào)計(jì)算出磁盤inode在文件系統(tǒng)中的塊號(hào)block
4.根據(jù)塊號(hào)block 通過sb_bread讀取緩沖區(qū)塊到內(nèi)存
5.根據(jù)inode表中的字節(jié)偏移offset 計(jì)算出 磁盤inode在塊中偏移
6.通過讀取的緩沖區(qū)和磁盤inode在塊中偏移 最終返回磁盤inode結(jié)構(gòu)
我們已知:
每個(gè)塊組inode個(gè)數(shù):EXT2_INODES_PER_GROUP(sb) = 16
磁盤inode大小:EXT2_INODE_SIZE(sb) = 128
塊大小的bit表示:EXT2_BLOCK_SIZE_BITS(sb) = 10
所以計(jì)算根inode塊號(hào):
- ino=2 跟inode時(shí):
- 塊組編號(hào): block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb) = (2 - 1) / 16 = 0
- inode表中的根inode偏移 : offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb
- = ( 1 % 16 ) * 128 = 128 =0x80 (第2個(gè)inode 也就是0x1480處)
- 文件系統(tǒng)中的根inode所在塊號(hào) : block = le32_to_cpu(gdp->bg_inode_table) + (offset >> EXT2_BLOCK_SIZE_BITS(sb))
- = 5 + (128 >> 10) = 5
- 根inode所在塊中偏移:offset &= (EXT2_BLOCK_SIZE(sb) - 1) = 128 = 0x80
- inode中位置 = bh->b_data + offset = 所在塊 + 0x80
所以:根inode所在的鏡像文件中偏移為:5 * 0x400 + 0x80 = 0x1400 + 0x80 = 0x1480
- 查看0x1480偏移處內(nèi)容如下(即是根目錄的磁盤inode內(nèi)容):
- *
- 00001480 ed 41 00 00 00 04 00 00 54 ff ad 60 44 ff ad 60 |.A......T..`D..`|
- 00001490 44 ff ad 60 00 00 00 00 00 00 04 00 02 00 00 00 |D..`............|
- 000014a0 00 00 00 00 01 00 00 00 07 00 00 00 00 00 00 00 |................|
- 000014b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
對(duì)照ext2文件系統(tǒng)磁盤inode結(jié)構(gòu),可知i_block為磁盤inode結(jié)構(gòu)的偏移40B處,內(nèi)容即為0x07(ext2通過i_block來查找文件在磁盤中的位置)。
于是我們知道,根目錄數(shù)據(jù)塊的塊號(hào) 為0x7(鏡像中字節(jié)偏移為 0x400 * 7= 1c00),這個(gè)數(shù)據(jù)塊中保存的是根目錄中包含的所有目錄和文件的目錄項(xiàng)(我們知道這里為"."、".."、"dir"、"lost+found"四個(gè)目錄項(xiàng))。
- 根目錄數(shù)據(jù)塊的內(nèi)容:
- *
- 00001c00 02 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00 |................|
- 00001c10 0c 00 02 02 2e 2e 00 00 0b 00 00 00 14 00 0a 02 |................|
- 00001c20 6c 6f 73 74 2b 66 6f 75 6e 64 00 00 0c 00 00 00 |lost+found......|
- 00001c30 d4 03 03 02 64 69 72 00 00 00 00 00 00 00 00 00 |....dir.........|
- 00001c40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
根據(jù)目錄項(xiàng)ext2_dir_entry_2 結(jié)構(gòu)我們可以查詢到文件名為dir的目錄項(xiàng),從而獲取dir目錄的inode號(hào),為0x0c(和我們之前通ls -lai顯示的dir目錄inode號(hào)12是一致)。
2)查找dir目錄
和上面查詢根inode一樣的原理,計(jì)算過程如下:
- dir目錄所在塊組編號(hào): block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb) = (12 - 1) / 16 = 0
- dir目錄所在inode表中的inode偏移 : offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb
- = ( 11 % 16 ) * 128 = 1408(0x580)
- 文件系統(tǒng)中的dir目錄的inode所在塊號(hào) : block = le32_to_cpu(gdp->bg_inode_table) + (offset >> EXT2_BLOCK_SIZE_BITS(sb))
- = 5 + (1408 >> 10) = 5 +1 =6
- dir目錄的inode所在塊號(hào)中偏移:offset &= (EXT2_BLOCK_SIZE(sb) - 1) = 1408(0x580)& (0x400 -1) = 0x180
- inode中位置 = bh->b_data + offset = 所在塊 + 0x180
所以:dir目錄inode所在的鏡像文件中字節(jié)偏移為:6 * 0x400 + 0x180 = 0x1800 + 0x180 = 0x1980
- 查看0x1980偏移處內(nèi)容如下(即是dir目錄的磁盤inode內(nèi)容):
- *
- 00001980 ed 41 00 00 00 04 00 00 84 ff ad 60 66 ff ad 60 |.A.........`f..`|
- 00001990 66 ff ad 60 00 00 00 00 00 00 02 00 02 00 00 00 |f..`............|
- 000019a0 00 00 00 00 02 00 00 00 63 00 00 00 00 00 00 00 |........c.......|
- 000019b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
對(duì)照ext2文件系統(tǒng)磁盤inode結(jié)構(gòu),可知i_block為磁盤inode結(jié)構(gòu)的偏移40B處,內(nèi)容即為0x63。
于是我們知道,dir目錄數(shù)據(jù)塊的塊號(hào) 為0x63(偏移為 0x400 * 0x63= 0x18c00),這個(gè)數(shù)據(jù)塊中保存的是dir目錄中包含的所有目錄和文件的目錄項(xiàng)(我們知道這里為"."、".."、"test.txt"三個(gè)目錄項(xiàng))。
- dir目錄數(shù)據(jù)塊的內(nèi)容:
- *
- 00018c00 0c 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00 |................|
- 00018c10 0c 00 02 02 2e 2e 00 00 0d 00 00 00 e8 03 08 01 |................|
- 00018c20 74 65 73 74 2e 74 78 74 00 00 00 00 00 00 00 00 |test.txt........|
- 00018c30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
對(duì)照目錄項(xiàng)ext2_dir_entry_2 結(jié)構(gòu),查找文件名為test.txt的inode號(hào),即為0x0d(和我們之前通ls -lai顯示的dir目錄inode號(hào)13是一致)。
于是我們知道,test.txt文件的inode號(hào)為0x0d(13)。
2)查找test.txt文件
和上面查詢根inode一樣的原理,計(jì)算過程如下:
- test.txt文件inode所在塊組 編號(hào): block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb) = (13 - 1) / 16 = 0
- test.txt文件inode在 inode表中的inode偏移 : offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb
- = ( 12 % 16 ) * 128 = 1536(0x600)
- 文件系統(tǒng)中的test.txt文件inode所在塊號(hào) : block = le32_to_cpu(gdp->bg_inode_table) + (offset >> EXT2_BLOCK_SIZE_BITS(sb))
- = 5 + (1536 >> 10) = 5 +1 =6
- test.txt文件inode所在塊號(hào)中偏移:offset &= (EXT2_BLOCK_SIZE(sb) - 1) = 1408(0x600)& (0x400 -1) = 0x200
- inode中位置 = bh->b_data + offset = 所在塊 + 0x200
所以:test.txt文件inode所在的鏡像文件中偏移為:= 6 * 0x400 + 0x200 = 0x1800 + 0x200 = 0x1a00
- 查看 0x1a00偏移處內(nèi)容如下(即是test.txt文件的磁盤inode內(nèi)容):
- 00001a00 a4 81 00 00 06 00 00 00 85 ff ad 60 66 ff ad 60 |...........`f..`|
- 00001a10 66 ff ad 60 00 00 00 00 00 00 01 00 02 00 00 00 |f..`............|
- 00001a20 00 00 00 00 01 00 00 00 15 00 00 00 00 00 00 00 |................|
- 00001a30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
對(duì)照ext2文件系統(tǒng)磁盤inode結(jié)構(gòu),可知i_block為磁盤inode結(jié)構(gòu)的偏移40B處,內(nèi)容即為0x15。
于是我們知道,test.txt文件數(shù)據(jù)塊的塊號(hào) 為0x15(偏移為0x15 * 0x400 = 0x5400)。
查看 0x5400偏移處內(nèi)容如下(test.txt文件數(shù)據(jù)塊的內(nèi)容):
- 查看 0x5400偏移處內(nèi)容如下(test.txt文件數(shù)據(jù)塊的內(nèi)容):
- *
- 00005400 68 65 6c 6c 6f 0a 00 00 00 00 00 00 00 00 00 00 |hello...........|
- 00005410 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
- *
最終可以看到文件數(shù)據(jù)為"hello"。
4)查找過程圖解
以下為 dir/test.txt查找過程:
已知根目錄inode號(hào)(ext2為2) -> 查找根目錄磁盤inode(文件系統(tǒng)掛載時(shí)查找) -> 查找根目錄的數(shù)據(jù)塊 -> 查找dir目錄的目錄項(xiàng)找到其inode號(hào) (為12) -> 查找dir目錄的磁盤inode -> 查找dir目錄的數(shù)據(jù)塊 -> 查找test.txt文件的inode號(hào)(為13) -> 查找test.txt文件的磁盤inode -> 查找test.txt文件的數(shù)據(jù)塊
下面為查找圖解:
4.總結(jié)
對(duì)于ext2文件系統(tǒng),路徑名查找中,實(shí)際上是解析路徑名的各個(gè)分量,查找每個(gè)分量的目錄項(xiàng),然后通過目錄項(xiàng)找到inode號(hào),通過inode號(hào)找到對(duì)應(yīng)的磁盤inode,然后通過磁盤inode獲得目錄/文件的數(shù)據(jù)塊, 最終查找到對(duì)應(yīng)目錄/文件的磁盤inode,而磁盤inode的i_block中保存著文件的邏輯塊號(hào)和磁盤的邏輯塊號(hào)映射關(guān)系,讀寫文件時(shí)就可以訪問到整個(gè)文件。