block大小和分区最大容量单个文件最大容量的关系

 原文新地址: http://cn.linux.vbird.org/linux_basic/0230filesystem.php

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 的结构画一下好了。

        

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.(由此可见分区大小与块没有关系,不知道我理解的对不对,这应该是系统内核的限制)

原文地址:https://www.cnblogs.com/caiyao/p/4606638.html