• ext3

Right now the maximum possible number of sub directories contained in a single directory in ext3 is 32000, which the dir_index is fixed.

  • ext4

Limit is 65000 subdirs for ext4 without "dir_index" enabled.

The 65000 subdir limit can be exceeded by turning on the "dir_nlink" feature of the filesystem with tune2fs -O dir_nlink, to allow an "unlimited" number of subdirs (subject to other directory limits, about 10-12M entries for 16-char filenames).

较新版本的 e2fsprogs 在格式化时一般会自动为 ext4 文件系统开启 dir_indexdir_nlink 特性。

File count limits for the current ext4 directory size with 20M+ entries.

ext4 Directory Indexing

A linear array of directory entries isn’t great for performance, so a new feature was added to ext3 to provide a faster (but peculiar) balanced tree keyed off a hash of the directory entry name. If the EXT4_INDEX_FL (0x1000) flag is set in the inode, this directory uses a hashed btree (htree) to organize and find directory entries.

With debugfs tool, information can be read out of the file system. Basic problem is simple, which directory is affected, how could this happen:

$ cd /var/
$ debugfs
debugfs> open /dev/sdd1
debugfs> cd log/
debugfs> htree .
Number of Entries (count): 508
Number of Entries (limit): 508

htree command lists the index in the folder and information about the hashed B-Tree.

The other potential problem is if you create and delete a large number of files from this directory, then the hash tables can become full and the leaf blocks are imbalanced and some become full even when many others are not (htree only has an average leaf fullness of 3/4 of each block).

This could probably happen if you have more than 5M files in a long-lived directory in your backup fs. This can be fixed (for some time at least) via e2fsck -fD on the unmounted filesystem to compact the directories.

fsck.ext4 -yfD /dev/sdd1

Directory Index 满时在文件夹下创建新文件可能会遇到这种错误:

EXT4-fs warning (device dm-0): ext4_dx_add_entry:2006: Directory index full!

这时就需要删除或者移动一些文件,或者尝试进行 e2fsck 操作。