The Journaling Block Device layer (JBD) isn’t ext3 specific. It was designed to add journaling capabilities to a block device. The ext3 filesystem code will inform the JBD of modifications it is performing (called a transaction). The journal supports the transactions start and stop, and in case of a crash, the journal can replay the transactions to quickly put the partition back into a consistent state.
JBD can handle an external journal on a block device. There’re 3 different data modes.
This is classic metadata-only journalling. Ext3 does not journal data at all. This mode provides a similar level of journaling as that of XFS and ReiserFS in its default mode – metadata journaling. File data is written back to the main fs lazily.
After a crash+recovery the fs structural integrity is preserved, but the contents of files can and will contain old, stale data, which may cause incorrect data to appear in files which were written shortly before the crash. Potentially hundreds of megabytes of it. This mode will typically provide the best ext3 performance.
The fs ensures that file data is written into the main fs prior to committing its metadata. Hence after a crash+recovery, your files will contain the correct data.
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.
Ordered is the default operating mode and throughput is good. It adds about one second to a four minute kernel compile when compared with ext2. Under heavier loads the difference becomes larger.
All data (including metadata) is written to the journal before it is released to the main fs for writeback.
This is a specialised mode – for normal fs usage you’re better off using ordered data, which has the same benefits of not corrupting data after crash+recovery. However for applications which require synchronous operation such as mail spools and synchronously exported NFS servers, this can be a performance win.
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.
In most cases, users write data by extending off the end of a file. Only in a few cases (such as databases) do users ever write into the middle of an existing file. Even overwriting an existing file is done by first truncating the file and then extending it again.
If the system crashes during such an extend in data=ordered mode, then the data blocks may have been partially written, but the extend will not have been, so the incompletely-written data blocks will not be part of any file.
The only way to get mis-ordered data blocks in data=ordered mode after a crash is if a program was overwriting in the middle of an existing file at the time of the crash. In such a case there is no absolute guarantee about write ordering unless the program uses fsync() or O_SYNC to force writes in a particular order.