On Linux, (perhaps as a function of the filesystem block size), when I create a directory and stat
it, it returns a size of 4096. I can create files in this directory, up to a point, without increasing the perceived size of the directory (as reported by stat
).
At some point, as the directory fills up with many files, the directory size balloons (I am not talking about the contents of the directory, I am talking about the blocks consumed to represent the directory itself). If files are deleted, the directory size remains the same.
Here's a quick example:
[root@uxlabtest:/]$ mkdir test
[root@uxlabtest:/]$ stat test
File: `test'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 1396685 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2011-07-26 14:06:04.000000000 -0400
Modify: 2011-07-26 14:06:04.000000000 -0400
Change: 2011-07-26 14:06:04.000000000 -0400
Then touch a bunch of files:
[root@uxlabtest:/]$ for i in `seq 1 10000`; do touch /test/$i; done
[root@uxlabtest:/]$ stat test
File: `test'
Size: 155648 Blocks: 312 IO Block: 4096 directory
Device: fd00h/64768d Inode: 1396685 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2011-07-26 14:06:04.000000000 -0400
Modify: 2011-07-26 14:06:56.000000000 -0400
Change: 2011-07-26 14:06:56.000000000 -0400
Then delete the files:
[root@uxlabtest:/]$ rm -rf /test/*
[root@uxlabtest:/]$ stat test
File: `test'
Size: 155648 Blocks: 312 IO Block: 4096 directory
Device: fd00h/64768d Inode: 1396685 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2011-07-26 14:07:11.000000000 -0400
Modify: 2011-07-26 14:07:12.000000000 -0400
Change: 2011-07-26 14:07:12.000000000 -0400
My questions are:
- Why does the size/block count of a directory increase monotonically?
- Is this a function of the underlying filesystem or the Linux VFS?
- Can the directory size ever be reduced without deleting and recreating the directory?
- Bonus points: Point me at the kernel source code where this behavior is implemented.