標籤:
block大小和單個檔案最大容量的關係(文章來自鳥哥的Linux私房菜http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem_1.php#ps2)
我們約略來分析一下 inode / block 與檔案大小的關係好了。inode 要記錄的資料非常多,但偏偏又只有 128bytes 而已, 而 inode 記錄一個 block 號碼要花掉 4byte ,假設我一個檔案有 400MB 且每個 block 為 4K 時, 那麼至少也要十萬筆 block 號碼的記錄呢!inode 哪有這麼多可記錄的資訊?為此我們的系統很聰明的將 inode 記錄 block 號碼的地區定義為12個直接,一個間接, 一個雙間接與一個三間接記錄區。這是啥?我們將 inode 的結構畫一下好了。
圖1.3.2、inode 結構(注5)
最左邊為 inode 本身 (128 bytes),裡面有 12 個直接指向 block 號碼的對照,這 12 筆記錄就能夠直接取得 block 號碼啦! 至於所謂的間接就是再拿一個 block 來當作記錄 block 號碼的記錄區,如果檔案太大時, 就會使用間接的 block 來記錄編號。如 1.3.2 當中間接只是拿一個 block 來記錄額外的號碼而已。 同理,如果檔案持續長大,那麼就會利用所謂的雙間接,第一個 block 僅再指出下一個記錄編號的 block 在哪裡, 實際記錄的在第二個 block 當中。依此類推,三間接就是利用第三層 block 來記錄編號啦!
這樣子 inode 能夠指定多少個 block 呢?我們以較小的 1K block 來說明好了,可以指定的情況如下:
- 12 個直接指向: 12*1K=12K
由於是直接指向,所以總共可記錄 12 筆記錄,因此總額大小為如上所示;
- 間接: 256*1K=256K
每筆 block 號碼的記錄會花去 4bytes,因此 1K 的大小能夠記錄 256 筆記錄,因此一個間接可以記錄的檔案大小如上;
- 雙間接: 256*256*1K=2562K
第一層 block 會指定 256 個第二層,每個第二層可以指定 256 個號碼,因此總額大小如上;
- 三間接: 256*256*256*1K=2563K
第一層 block 會指定 256 個第二層,每個第二層可以指定 256 個第三層,每個第三層可以指定 256 個號碼,因此總額大小如上;
- 總額:將直接、間接、雙間接、三間接加總,得到 12 + 256 + 256*256 + 256*256*256 (K) = 16GB
此時我們知道當檔案系統將 block 格式化為 1K 大小時,能夠容納的最大檔案為 16GB,比較一下檔案系統限制表的結果可發現是一致的!但這個方法不能用在 2K 及 4K block 大小的計算中, 因為大於 2K 的 block 將會受到 Ext2 檔案系統本身的限制,所以計算的結果會不太符合之故。
block大小和分區最大容量的關係(來自wikipedia)
File system limits[edit]
Theoretical ext2 limits under Linux
[4]
| Block size: |
1 KiB |
2 KiB |
4 KiB |
8 KiB |
| max. file size: |
16 GiB |
256 GiB |
2 TiB |
2 TiB |
| max. filesystem size: |
4 TiB |
8 TiB |
16 TiB |
32 TiB |
The reason for some limits of ext2 are the file format of the data and the operating system‘s kernel. Mostly these factors will be determined once when the file system is built. They depend on the block size and the ratio of the number of blocks and inodes. In Linux the block size is limited by the architecture page size.(塊大小的限制來自page size,常用塊大小為1 2 3 8)
There are also some userspace programs that can‘t handle files larger than 2 GiB.
If b is the block size, the maximum file size is limited to min( ((b/4)3+(b/4)2+b/4+12)*b, (232-1)*512 ) due to the i_block structure (an array of direct/indirect EXT2_N_BLOCKS) and i_blocks (32-bit integer value) representing the number of 512-byte "blocks" in the file.(計算塊大小和單個檔案最大容量的關係)
The max number of sublevel-directories is 31998, due to the link count limit. (由於目錄串連數的限制子目錄的最大數量為31998)Directory indexing is not available in ext2, so there are performance issues for directories with a large number of files (10,000+). The theoretical limit on the number of files in a directory is 1.3 × 1020 由於在ext2檔案系統中沒有使用檔案索引,所以當在一個檔案夾下出現檔案數量超過10000時會出現問題,理論上檔案數限制在1.3*10^20 , although this is not relevant for practical situations.
Note: In Linux 2.4 and earlier, block devices were limited to 2 TiB, limiting the maximum size of a partition, regardless of block size.(由此可見分區大小與塊沒有關係,不知道我理解的對不對,這應該是系統核心的限制)
block大小和分區最大容量單個檔案最大容量的關係