Ext2文件系统

参考《深入理解Linux内核》 第十七章

针对ext2文件系统,先说下对当个文件大小限制

Block 大小 1KB 2KB 4KB
最大單一檔案限制 16GB 256GB 2TB
最大檔案系統總容量 2TB 8TB 16TB

示例计算大小


ext2以一种特殊的文件实现了目录,这种文件的数据块存放了文件名和吸纳高硬度索引节点号。特别说明的是,这样的数据块包含了类型为ext2_dir_entry_2的结构。下表列出了这个结构的域,因为该结构最后一个名字域是最大为ext2_name_len(通常是255)个字符的边长数组,因此这个结构的长度是可变的。此外,因为效率的原因,一个目录项的长度总是4的倍数,并在必要时用null自读(\0) 填充文件名的末尾。name_len域存放实际的文件名长度

类型 描述
__u32 inode 索引节点号
__u16 rec_len 目录项长度
__u8 name_len 文件名长度
__u8 file_type 文件类型
char [ext2_name_len] name 文件名

file_type域存放制定的文件类型的值, rec_len域可以被解释为指向下一个有效目录项的指针:他是偏移量,与目录项的起始地址相加就得到下一个有效目录项的起始地址。为了删除一个目录项,把他的inode域置为0并适当地增加前一个有效目录项rec——len域的值就足够了。仔细看一下图 rec_len域,你会发现”oldfile“项已被删除,因为"usr"的"rec_len"域被置为12+16 ("usr"和 "odlfile"目录项的长度)

                      file_type---+
                                  |
                  name_len---+    |
                             |    |
    inode          rec_len   |    |       name
+-----------------+--------+----+----+----+----+----+----+
|    21           |   12   |  1 | 2  | .  | \0 | \0 | \0 |
+-----------------+--------+----+----+----+----+----+----+
|    22           |   12   |  2 | 2  | .  | .  | \0 | \0 |
+-----------------+--------+----+----+----+----+----+----+----+----+----+----+
|    53           |   16   |  5 | 2  | h  | o  | m  | e  | 1  | \0 | \0 | \0 |
+-----------------+--------+----+----+----+----+----+----+----+----+----+----+
|    67           |   28   |  3 | 2  | u  | s  | r  | \0 |
+-----------------+--------+----+----+----+----+----+----+----+----+----+----+
|    0            |   16   |  7 | 1  | o  | l  | d  | f  | i  |  l |  e | \0 |
+-----------------+--------+----+----+----+----+----+----+----+----+----+----+
|    34           |   12   |  4 | 2  | s  | b  | i  | n  |
+-----------------+--------+----+----+----+----+----+----+ 

下面做个简单的测试
先随机生成84个40字符长度的文件 按照前面的介绍占用空间(84个文件加两个目录)应该有 84(40+8) + (8 + 4)2= 4056

mkdir /tmp/dir1 && for num in `seq 1 84`; do mktemp --tmpdir=/tmp/dir1 --suffix=.txt  dd79fcba-c737-4854-ba33-bb222ceab513.XXX;done

看下目前的文件目录大小

drwxr-xr-x  2 root root 4096 Oct 10 19:07 ./
drwxrwxrwt 11 root root 4096 Oct 10 19:07 ../
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_11gCHL.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_1ZiBMU.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_2DMtQw.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_30VwjB.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_34bGTx.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_3BdNFC.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_3X9Bfu.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_4WHZMV.txt
.......

这样还有 4096-4056=40
我们在生成一个32字符长度的文件

touch 12345678901234567890123456789012

这样再看下文件夹大小

drwxr-xr-x  2 root root 4096 Oct 10 19:08 ./
drwxrwxrwt 11 root root 4096 Oct 10 19:07 ../
-rw-r--r--  1 root root    0 Oct 10 19:08 12345678901234567890123456789012
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_11gCHL.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_1ZiBMU.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_2DMtQw.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_30VwjB.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_34bGTx.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_3BdNFC.txt
......

删除刚创建的32字符长度文件后建立一个30字符长度的文件

rm 12345678901234567890123456789012

touch 123456789012345678901234567890

drwxr-xr-x  2 root root 4096 Oct 10 19:11 ./
drwxrwxrwt 11 root root 4096 Oct 10 19:10 ../
-rw-r--r--  1 root root    0 Oct 10 19:11 123456789012345678901234567890
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_11gCHL.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_1ZiBMU.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_2DMtQw.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_30VwjB.txt
-rw-------  1 root root    0 Oct 10 19:07 random_forty_length_file_name_34bGTx.txt
......

目录大小保持4096, 这样删除30字符大小文件,建立33字符长度文件

rm 123456789012345678901234567890

touch 123456789012345678901234567890123

drwxr-xr-x  2 root root 12288 Oct 10 19:13 ./
drwxrwxrwt 11 root root  4096 Oct 10 19:10 ../
-rw-r--r--  1 root root     0 Oct 10 19:13 123456789012345678901234567890123
-rw-------  1 root root     0 Oct 10 19:07 random_forty_length_file_name_11gCHL.txt
-rw-------  1 root root     0 Oct 10 19:07 random_forty_length_file_name_1ZiBMU.txt
-rw-------  1 root root     0 Oct 10 19:07 random_forty_length_file_name_2DMtQw.txt
-rw-------  1 root root     0 Oct 10 19:07 random_forty_length_file_name_30VwjB.txt
-rw-------  1 root root     0 Oct 10 19:07 random_forty_length_file_name_34bGTx.txt
......

按照计算 (40+8)84 + (32+8)1 + (4+8)*2 = 4096
所以建立33个字符长度的文件时,超出一个block大小, 需要另外申请block存储了,这里还有一个地方需要注意 文件系统并不是只申请一个block,而是一次性申请了两个 目录大小达到了 12288

posted @ 2017-10-10 18:47  mikeguan  阅读(426)  评论(0编辑  收藏  举报