Journaling - aryanjoshi0823/5143-Operating-System GitHub Wiki
Journaling
Journaling, or write-ahead logging, is a technique used to address file system inconsistencies in operating systems. Borrowed from database management systems, this method ensures system reliability by first recording a summary of intended actions into a log (or journal) before applying them to the disk. This process allows the operating system to recover efficiently in case of a crash by consulting the log, thus avoiding the need for lengthy disk scans, as required by utilities like FSCK. Examples of file systems employing journaling include Linux ext3 and ext4 and Windows NTFS.
How Data Journaling Works
The journal is a straightforward data structure used to store logs. It contains the following components:
- TxB (Transaction Begin Block): Contains the transaction ID (TID).
- Inode, Bitmap, and Data Blocks (Metadata): Includes copies of the blocks that need updating on the disk.
- TxE (Transaction End Block): Marks the end of the transaction, identified by the TID.
The journaling process includes the following stages:
- Journal Write: Write TxB, inode, bitmap, and data block contents into the log.
- Journal Commit: Write TxE into the log.
- Checkpoint: Write the inode, bitmap, and data block contents onto the disk.
Handling Crashes During Journaling
If a crash occurs, recovery depends on the stage of failure:
-
Before TxE: The transaction is incomplete and can be skipped, maintaining file system consistency.
-
After TxE: The transaction is logged but not fully written to the disk. In this case, the system scans the log, identifies recent transactions, and re-applies the last transaction to ensure consistency. This is known as redo logging.
The figure below shows an overall view of the journal, with tr1 as the oldest transaction and tr5 the newest.
Circular Buffer for Journaling
To prevent the journal from filling up due to multiple transactions, it is treated as a circular buffer. In this system:
- Newer transactions overwrite older ones in a circular manner.
- The superblock maintains pointers to the oldest and newest transactions.
- Completed transactions are marked as โfree,โ and the superblock updates to point to the next transaction.