I'm writing 100 MB files to a disk with ext4 filesystem continually, and when the disk is full, I just delete the oldest file and write the new one. The files never grow in side. When I do this the file system becomes quiet fragmented after some time. I've looked into all formatting options for ext4, but haven't figured out why such fragmentation happens.
Basically, to simplify the matter, what I do is the following in a loop:
int32_t fid = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
int success = fallocate(fid, 0, 0, MAX_FILE_SIZE);
close(fid);
Is there a way to tell the file system to write the data to disk in contiguous blocks? I've looked at other posts, but I haven't quite figured it out. Should I be using malloc() instead fallocate()?
Ext4 has an optional support of "extents", that are contiguous blocks of disk. The support must be enabled with tune2fs. Here some useful links:
https://serverfault.com/questions/400026/linux-ext4-extents-attribute
https://www.kernel.org/doc/Documentation/filesystems/ext4.txt
http://ext2read.blogspot.it/