删除条目后,为什么包含大量条目的目录不会缩小?

Ser*_*nyy 12 directory ext4 inode

关闭投票通知:虽然链接的副本提出了相同的问题,但评论和唯一答案并未提供来自权威来源的答案。在由托马斯·迪基我的问题的答案,事实上确实提供我一直在寻求答案。因此,建议的重复没有帮助,但是托马斯在这里的回答是正确的。

根据G-Man在这个 PerlDuck 的评论中对超级用户问题和个人帐户的回答,具有大量条目的目录大小超过 4096 字节(可以通过ls -l输出看到),但是一旦条目被删除 -数字永远不会下降。

问题是“为什么”?是否由于ext4文件系统配置为保留目录元数据的方式?显然,删除目录并重新创建它不是解决方案,因为它会删除原始 inode 并创建一个新的 inode。可以做些什么来手动减少数量?

Tho*_*key 15

引用开发人员的话(在 linux 内核线程中ext3/ext4 目录在删除大量文件后不会缩小):

On Thu, May 14, 2009 at 08:45:38PM -0400, Timo Sirainen wrote:
>
> I was rather thinking something that I could run while the system was  
> fully operational. Otherwise just moving the files to a temp directory +
> rmdir() + rename() would have been fine too.
>
> I just tested that xfs, jfs and reiserfs all shrink the directories  
> immediately. Is it more difficult to implement for ext* or has no one  
> else found this to be a problem?

It's probably fairest to say no one has thought it worth the effort.
It would require some fancy games to swap out block locations in the
extent trees (life would be easier with non-extent-using inodes), and
in the case of htree, we would have to keep track of the index block
so we could remove it from the htree index.  So it's all doable, if a
bit tricky in terms of the technical details; it's just that the
people who could do it have been busy enough with other things.

It's hasn't been considered high priority because most of the time
directories don't go from holding thousands of files down to a small
handful.  

                                                - Ted
Run Code Online (Sandbox Code Playgroud)