Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing consistent data to file in Linux

Tags:

linux

I want to write a library which logs data to a file. Unfortunately, my system suffers from unexpected reboots and power loss. Does Linux write operation to a file guaranties that my file will always contain consistent data? Does it guarantee "all or nothing"?

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

Thanks.

like image 672
michael Avatar asked Apr 29 '26 20:04

michael


1 Answers

When you mount the file system you can specify one of the below options. It seems like the third one suits 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 journal data at all. This mode provides a similar level of journaling as that of XFS, JFS, and ReiserFS in its default mode - metadata journaling. A crash+recovery can cause incorrect data to appear in files which were written shortly before the crash. This mode will typically provide the best ext3 performance.

  • ordered mode In data=ordered mode, ext3 only officially journals metadata, but it logically groups metadata and data blocks into a single unit called a transaction. When it's time to write the new metadata out to disk, the associated data blocks are written first. In general, this mode performs slightly slower than writeback but significantly faster than journal mode.

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

like image 126
michael Avatar answered May 01 '26 13:05

michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!