I have a 900GB ext4 partition on a (magnetic) hard drive that has no defects and no bad sectors. The partition is completely empty except for an empty lost+found directory. The partition was formatted using default parameters except that I set the number of reserved filesystem blocks to 1%.I downloaded the ~900MB file xubuntu-15.04-desktop-amd64.iso to the partition's mount point directory using wget. When the download was finished, I found that the file was split into four fragments:filefrag -v /media/emma/red/xubuntu-15.04-desktop-amd64.isoFilesystem type is: ef53File size of /media/emma/red/xubuntu-15.04-desktop-amd64.iso is 1009778688 (246528 blocks of 4096 bytes) ext:     logical_offset:        physical_offset: length:   expected: flags:   0:        0..   32767:      34816..     67583:  32768:               1:    32768..   63487:      67584..     98303:  30720:               2:    63488..   96255:     100352..    133119:  32768:      98304:   3:    96256..  126975:     133120..    163839:  30720:               4:   126976..  159743:     165888..    198655:  32768:     163840:   5:   159744..  190463:     198656..    229375:  30720:               6:   190464..  223231:     231424..    264191:  32768:     229376:   7:   223232..  246527:     264192..    287487:  23296:             eof/media/emma/red/xubuntu-15.04-desktop-amd64.iso: 4 extents foundThinking this might be releated to wget somehow, I removed the ISO file from the partition, making it empty again, then I copied the ~700MB file v1.mp4 to the partition using cp. This file was fragmented too. It was split into three fragments:filefrag -v /media/emma/red/v1.mp4Filesystem type is: ef53File size of /media/emma/red/v1.mp4 is 737904458 (180153 blocks of 4096 bytes) ext:     logical_offset:        physical_offset: length:   expected: flags:   0:        0..   32767:      34816..     67583:  32768:               1:    32768..   63487:      67584..     98303:  30720:               2:    63488..   96255:     100352..    133119:  32768:      98304:   3:    96256..  126975:     133120..    163839:  30720:               4:   126976..  159743:     165888..    198655:  32768:     163840:   5:   159744..  180152:     198656..    219064:  20409:             eof/media/emma/red/v1.mp4: 3 extents foundWhy is this happening? And is there a way to prevent it from happening? I thought ext4 was meant to be resistant to fragmentation. Instead I find that it immediately fragments a solitary file when all the rest of the volume is unused. This seems to be worse than both FAT32 and NTFS.
Why are these files in an ext4 volume fragmented?
filesystems;ext4;large files
3 or 4 fragments in a 900mb file is very good.  Fragmentation becomes a problem when a file of that size has more like 100+ fragments.  It isn't uncommon for fat or ntfs to fragment such a file into several hundred pieces.You generally won't see better than that at least on older ext4 filesystems because the maximum size of a block group is 128 MB, and so every 128 MB the contiguous space is broken by a few blocks for the allocation bitmaps and inode tables for the next block group.  A more recent ext4 feature called flex_bg allows packing a number of ( typically 16 ) block groups' worth of these tables together, leaving longer runs of allocatable blocks but depending on your distribution and what version of e2fsprogs was used to format it, this option may not have been used.You can use tune2fs -l to check the features enabled when your filesystem was formatted.