Question
How can I update a file atomically without requiring my program to wait for slow physical media (such as with fsync
)?
My hope is that the OS could "buffer" the typical fsync
and rename
operations in RAM, and then write them to disk in the proper order whenever it is convenient.
Background
I am developing software that runs in a custom embedded Linux environment with an ext4
filesystem. The program makes periodic updates to a file on disk. I need to maintain the integrity of this file without sacrificing application performance.
From what I have read, the accepted practice for safely updating a file is as follows:
- create a new temp file
- write data to the temp file
fsync()
the temp file- rename the temp file to the appropriate name
fsync()
the containing directory
This process makes sense to me, but in my particular application, I would like to avoid a blocking call to fsync()
. I don't care when the data is written to disk, so long as the file is always in a valid state. If the file is out-of-date, that is OK.
What I've Learned So Far
It seems that there is already quite a bit of discussion around ext4
and the proper use of fsync
. If I understand correctly, I might be able to forgo the use of fsync
if auto_da_alloc
is enabled for my filesystem (link), but I'm not convinced that is the best solution.