2024-06-07 16:25:22

by Nic Bretz

[permalink] [raw]
Subject: stat Size and Blocks numbers don't decrease after deletion

When I create a new directory and new files in it, I see its size and
blocks increase when running stat. After deleting all files, the size
and blocks of the directory don't decrease in stat.

I was thinking that ext4 runs defragmentation in the background and
eventually those two numbers will decrease. It looks like they don't.

Is this the intended behavior? Thanks
Nic


2024-06-10 20:55:19

by Andreas Dilger

[permalink] [raw]
Subject: Re: stat Size and Blocks numbers don't decrease after deletion

On Jun 7, 2024, at 10:25 AM, Nic Bretz <[email protected]> wrote:
>
> When I create a new directory and new files in it, I see its size and
> blocks increase when running stat. After deleting all files, the size
> and blocks of the directory don't decrease in stat.

Correct, ext4 does not shrink directories after they have been populated.
There was a patch series at https://patchwork.ozlabs.org/project/linux-ext4/patch/[email protected]/ that fell
into a crack and was never landed. However, I think that patch would
still be interesting to revive.

> I was thinking that ext4 runs defragmentation in the background and
> eventually those two numbers will decrease. It looks like they don't.

No, ext4 does not do any kind of automatic background scanning or
filesystem changes. You can run (offline) "e2fsck -fD" if there is
a big problem with large empty directories.

Cheers, Andreas






Attachments:
signature.asc (890.00 B)
Message signed with OpenPGP

2024-06-11 13:32:10

by Nic Bretz

[permalink] [raw]
Subject: Re: stat Size and Blocks numbers don't decrease after deletion

On Mon, Jun 10, 2024 at 1:55 PM Andreas Dilger <[email protected]> wrote:
>
> On Jun 7, 2024, at 10:25 AM, Nic Bretz <[email protected]> wrote:
> >
> > When I create a new directory and new files in it, I see its size and
> > blocks increase when running stat. After deleting all files, the size
> > and blocks of the directory don't decrease in stat.
>
> Correct, ext4 does not shrink directories after they have been populated.
> There was a patch series at https://patchwork.ozlabs.org/project/linux-ext4/patch/[email protected]/ that fell
> into a crack and was never landed. However, I think that patch would
> still be interesting to revive.
>
> > I was thinking that ext4 runs defragmentation in the background and
> > eventually those two numbers will decrease. It looks like they don't.
>
> No, ext4 does not do any kind of automatic background scanning or
> filesystem changes. You can run (offline) "e2fsck -fD" if there is
> a big problem with large empty directories.
>
> Cheers, Andreas
>

Thank you for taking the time to explain this. I was thinking to
possibly start looking into this, but if there is already a patch,
I'll study that.
Nic