Fragmentation seems to create a lot of unnecessary seeks when traversing a directory tree on a HDD:
# stat -c %F 00 01 02
directory
directory
directory
# filefrag -v 00 01 02
Filesystem type is: ef53
File size of 00 is 12288 (3 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 0: 428351942.. 428351942: 1:
1: 1.. 2: 428352760.. 428352761: 2: 428351943: last,eof
00: 2 extents found
File size of 01 is 12288 (3 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 0: 428351771.. 428351771: 1:
1: 1.. 2: 428891667.. 428891668: 2: 428351772: last,eof
01: 2 extents found
File size of 02 is 12288 (3 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 0: 428351795.. 428351795: 1:
1: 1.. 2: 428352705.. 428352706: 2: 428351796: last,eof
02: 2 extents found
e4defrag isn't able to defrag them
# e4defrag -v 00
ext4 defragmentation for directory(00)
[1/116] "00"
File is not regular file [ NG ]
So how do I defragment a directory? Not its contents, but the directory itself. The directories are in use, so it should be done atomically, just like defragmenting regular files does not interfere with their use.