Writing consistent data to a file on Linux

I want to write a library that writes data to a file. Unfortunately my system suffers from unexpected restarts and power loss. Does Linux write to the file ensure that my file always contains consistent data? Does it guarantee "all or nothing"?

If so, is there a limit on the size of the data being written?

Thank.

+3


source to share


2 answers


When you mount a filesystem, you can specify one of the following options. It looks like the third fits my requirements. This is what I found at http://lxr.free-electrons.com/source/Documentation/filesystems/ext3.txt

Data mode



There are 3 different data modes:

  • writeback mode In data = writeback mode, ext3 does not write any log data at all. This mode provides a similar level of logging as for XFS, JFS and ReiserFS, the default mode is metadata logging. Failure + recovery may result in incorrect data. appear in files that were written shortly before the accident. This mode will generally provide the best ext3 performance.

  • ordered mode In data = order mode, ext3 only officially registers metadata, but logically groups of metadata and data blocks into a single unit called a transaction. when it is time to write new metadata to disk, the associated data blocks are written first. In general, this mode is slightly slower than but significantly faster than journal mode.

  • data = journal mode provides full data and metadata journaling. All new data is first written to the log and then to its final location. In the event of a failure, the log can be replayed, bringing the data and metadata to a consistent state. This mode is the slowest, except when data needs to be read and written to disk at the same time, when it beats all other modes.

+1


source


You can never predict where physical write operations will stop when the power is turned off. Even if you are using the functions of journaling

some file systems. Please note that the journal must also write.



0


source







All Articles