Why do ext2 and successors not need defragmentation?

One way in which the ext4 file system keeps fragmentation in-check, is with the process of delayed allocation (ext4's default allocation mode).

Delayed allocation works by deferring the mapping of newly-written file data blocks to disk blocks in the filesystem until writeback time.

This works by allocating most of the blocks for a file at the same time, when the total number of blocks (or at least a ballpark) in each file is known. This allows the block allocator (the mballoc mentioned in MMK's answer) to do a better job of finding an appropriately-sized area of free space to put the file.


In ext3 file system, there is a block allocator for the disk for each block, and so, it is quite possible that fragmentation can occur.

However, in ext4 file system there is a multi-block allocator which can delay the writing of blocks to disk, so that it can property allocate several blocks at a time in a single chunk of disk to allow for a contiguous write - and so, it is less likely that fragmentation can occur (it is still possible, just less likely)


At least for Ext2 (and Ext3 I think, though I am less sure), there is nothing in the on-disk format/structure that would prevent defragmentation.

The lack of need for defragmentation is going reside at the implementation level, which will vary from OS to OS. That is, depending upon the implementation, a file in Ext2 may or may not be fragmented.

If fragmentation is avoided, that will likely be due to delayed allocation of physical blocks. That is, at some level of the implementation, the file system will cache the data (and access it via logical block number) until such time as it is written/committed to disk. When the data is finally written to disk, physical blocks must be allocated. The allocation algorithm may (or may not) allocate a number of contiguous available physical blocks for the data.

Hope this helps.